DailyJS

Managing Node.js Callback Hell, this Considered Harmful

Alex R. Young

Subscribe

@dailyjs

Facebook

Google+

tutorials node

Managing Node.js Callback Hell, this Considered Harmful

Posted by Alex R. Young on .
Featured

tutorials node

Managing Node.js Callback Hell, this Considered Harmful

Posted by Alex R. Young on .

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 doAsync2 happened before 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.

Tom Boutell sent in a blog post about OO in JavaScript, called "this" considered harmful (sometimes):

Most JavaScript implementations of "classes" do have certain features in common. They rely on the "this" keyword to refer to the current object; after all, it's built into the language. They provide a convenience function to implement subclassing, because it's tricky to get right, especially in older browsers. And they have a really tough time handling callbacks in methods, because "this" ceases to refer to the object you expect.

Tim Oxley posted some comments discussing how to use Function.prototype.bind as well.