Using Generators with Promises

Let’s return to our Express APIs for working with book bundles. So far we have ways of creating and retrieving bundles, and we can update the name property of a bundle.

Now we’ll make an API for adding books to a bundle using the PUT HTTP verb. This code will have to make several asynchronous calls: one to get the existing bundle, one to get book details, and one to put the bundle data back. To do all this, we’ll use promises and generators to flatten and simplify the chain of operations.

Using the PUT book API

Before we get into the code, let’s take a look at how this API works. That way, when we dive into the code it’ll be more clear what it’s doing.

To use the PUT book API, we need a bundle, so make ...

Get Node.js the Right Way now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.