Chapter 3. Asynchronous Control Flow Patterns with Callbacks
Moving from a synchronous programming style to a platform such as Node.js, where continuation-passing style and asynchronous APIs are the norm, can be frustrating. Writing asynchronous code can be a different experience, especially when it comes to control flow. Asynchronous code might make it hard to predict the order in which statements are executed within Node.js applications, so simple problems such as iterating over a set of files, executing tasks in sequence, or waiting for a set of operations to complete require the developer to take new approaches and techniques to avoid ending up writing inefficient and unreadable code. One common mistake is to fall into the trap of the callback hell problem and see the code growing horizontally rather than vertically, with a nesting that makes even simple routines hard to read and maintain.
In this chapter, we will see how it's actually possible to tame callbacks and write...