Marc Harter, who is working with me on Node.js in Practice, recently published Managing Node.js Callback Hell:
Callback hell is subjective, as heavily nested code can be perfectly fine sometimes. Asynchronous code is hellish when it becomes overly complex to manage the flow. A good question to see how much "hell" you are in is: how much refactoring pain would I endure if
doAsync1? The goal isn't about removing levels of indentation but rather writing modular (and testable!) code that is easy to reason about and resilient.
The example he uses is nested asynchronous file operations, with a counter to determine completion. This is compared to a version that uses async, and another that uses promises with q.
The post was originally published on StrongLoop's blog, here: Managing Node.js Callback Hell with Promises, Generators and Other Approaches. StrongLoop's blog is worth subscribing to if you're a Node developer -- it has general tutorials and coverage of interesting npm modules.
Tim Oxley posted some comments discussing how to use
Function.prototype.bind as well.