Chapter 4
Flow Control with Async.js

Up to this point, this book has been about using abstractions to manage async tasks throughout an application. PubSub, for instance, is an abstraction that lets us distribute events from their source to other layers of the application (e.g., from the view to the model). Promises are an abstraction that let us represent simple tasks with objects that can be combined to represent complex tasks. Together, these abstractions go a long way toward helping us solve the problem of callback spaghetti.

There’s still one weak spot in our armor, though: iteration. What do we do when we need to perform a series of I/O operations, either in series or in parallel? This is such a common problem in the Node world that it has a name: flow control (also called control flow). And the same way that Underscore.js can dramatically simplify (synchronous) iterative code, a good flow control library can strip away the boilerplate from your async code.

The most popular of these libraries is Caolan McMahon’s powerful Async.js.[38] In fact, as of this writing, Async.js is the third most required library in the npm registry,[39] sharing the limelight with superstars like Underscore.js and Express.

In this chapter, we’ll explore what Async.js can do in a Node setting. (Async.js can run in the browser, too, but few client-side apps need it.) We’ll also take a brief look at an alternative library, Tim Caswell’s sleek Step.[40]

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.9.172