I posted this as a comment on the blog post, though I would share it here.
This is awesome that you wrote a post on this, it is an important building block to proper node.js coding. I know I had trouble with it.
There is another way to handle asynchronous iteration, while similar to what you have done, gives a bit more flexibility and is abstracted out so you can use it anywhere.
On a side note, I wanted to say that people hell bent on turning node.js into another language should just use that language. There is nothing wrong with the way node.js handles asynchronous code, and really keeping everything asynchronous instead of building in stuff to fake synchronous code is bad for node.js and the whole ecosystem. It promotes bad habits and bad code. If you can't handle callbacks or events, then maybe you should go back to using whatever you are more comfortable with, or write your own abstractions.
Your last paragraph comes off as rather dismissive. How does abstracting away the control flow promote bad code? Programers should only need to worry about the control flow of their program when they absolutely must (which is pretty rare) otherwise the compiler (or interpretor) should be able to take care of those details. Its not that people can't handle callbacks or events, its that they should not have to for most of the things node forces you to use them for (everything, all the time).
Granted it was a bit dismissive, but it is a common issue of people coming into the node.js community and saying it needs this or that. One main thing being that you shouldn't have to deal with callbacks/events. Now I think everyone is entitled to their own opinion on how things work, but the powers that be and the majority of senior node.js developers agree that the way it is handled is probably the best for now. Removing that control or mucking up the core of node.js so people can code like they do in other languages is not a good thing.
I know people like node.js and want to make it better in their own way, but I feel the course it is on is the best course for right now. It might add more cruft for developers, but there are plenty of abstractions that make it simple if you choose to.
Good points. I guess there are some of us who would prefer a different balance. In the end, I think node.js will continue to be a great project, just maybe not ideal for everything we might want. I'm pretty sure I'll continue using node.js either way.
Comments
I posted this as a comment on the blog post, though I would share it here.
This is awesome that you wrote a post on this, it is an important building block to proper node.js coding. I know I had trouble with it.
There is another way to handle asynchronous iteration, while similar to what you have done, gives a bit more flexibility and is abstracted out so you can use it anywhere.
https://gist.github.com/b5af7369ec9939ab7d94
This is using code from a script of mine as an example, but it should be pretty easy to see the pattern.
Here is your above serialized example with the above function I just showed you:
https://gist.github.com/26bda51358610667f9f3
---
On a side note, I wanted to say that people hell bent on turning node.js into another language should just use that language. There is nothing wrong with the way node.js handles asynchronous code, and really keeping everything asynchronous instead of building in stuff to fake synchronous code is bad for node.js and the whole ecosystem. It promotes bad habits and bad code. If you can't handle callbacks or events, then maybe you should go back to using whatever you are more comfortable with, or write your own abstractions.
Your last paragraph comes off as rather dismissive. How does abstracting away the control flow promote bad code? Programers should only need to worry about the control flow of their program when they absolutely must (which is pretty rare) otherwise the compiler (or interpretor) should be able to take care of those details. Its not that people can't handle callbacks or events, its that they should not have to for most of the things node forces you to use them for (everything, all the time).
Granted it was a bit dismissive, but it is a common issue of people coming into the node.js community and saying it needs this or that. One main thing being that you shouldn't have to deal with callbacks/events. Now I think everyone is entitled to their own opinion on how things work, but the powers that be and the majority of senior node.js developers agree that the way it is handled is probably the best for now. Removing that control or mucking up the core of node.js so people can code like they do in other languages is not a good thing.
I know people like node.js and want to make it better in their own way, but I feel the course it is on is the best course for right now. It might add more cruft for developers, but there are plenty of abstractions that make it simple if you choose to.
Good points. I guess there are some of us who would prefer a different balance. In the end, I think node.js will continue to be a great project, just maybe not ideal for everything we might want. I'm pretty sure I'll continue using node.js either way.