Managing Node.js Callback Hell with Promises, Generators and Other Approaches | nodeJS and Web APIs | Scoop.it

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.

In this article, we will write a module using a number of tools and libraries to show how control flow can work. We’ll even look at an up and coming solution made possible by the next version of Node.

Let’s say we want to write a module that finds the largest file within a directory.

* A nested approach
* A modular approach
* An async approach
* A promises approach
* A generators approach



Via Jan Hesse