We've already seen one approach to implement coroutines in the front-end using generators, in Chapter 4, Lazy Evaluation with Generators. In this section, we'll use the Co
library (https://github.com/tj/co) to implement coroutines. This library also relies on generators and promises.
We'll start by walking through the general premise of Co
, and then, we'll write some code that waits for asynchronous values using promises. We'll then look into the mechanics of transferring resolved values from a promise to our coroutine function, asynchronous dependencies, and creating coroutine utility functions.
At its core, the Co
library uses a co()
function to create a coroutine. In fact, its basic usage looks familiar to the coroutine function that we created earlier in this book. Here's what it looks like:
co(function* () { // TODO: co-routine amazeballs. });
Another similarity between the Co
library and our earlier coroutine implementation is that values are passed...