Callbacks vs generators

Before getting into the difference between generators and coroutines let’s look what makes generators useful in environments like Node.js or the browser where callbacks dominate the ecosystem.

First off generators are complementary to callbacks, some form of callback is required to “feed” the generators. These “futures”, “thunks”, or “promises” — whatever you prefer to call them allow deferred execution of some logic, this is what you yield a value and allow the generator to handle the rest.

Once one of these values is yielded to the caller, the caller waits around for the callback and then resumes the generator. Depending on how you look at this, generators used in this way are effectively the same mechanism as a callback, however with some added benefits that we’ll look at soon.

If you’re still not quite sure how generators come into the picture, here’s a simple implementation of a flow control library built on top of this facility:

Why coroutines make code more robust

Contrasting the typical browser or Node.js environment, coroutines run each “light-weight thread” with its own stack. The implementations of these threads varies but typically they have a relatively small initial stack size (~4kb), growing when required.

Why is this so great? Error handling! If you’ve worked with Node.js where exceptions are quite prevelant even compared to the browser, you’ll know that error handling is no simple feat. Sometimes you get multiple callbacks which have undefined side-effects, or forget a callback all-together and fail to properly handle or report the exception. Or perhaps you forgot to listen for an “error” event, in which case it becomes an uncaught exception and brings down the process.

Some people are happy with this process and that’s fine, however as someone who has used Node.js since its infancy, it’s my opinion there is much more that can be done to improve this flow. Node.js is great in so many other ways but this its achilles heel.

Let’s take a simple example of reading and writing to and from the same file with callbacks:

You might think this isn’t so bad, you see code like this all the time! Well it’s broken :) Why? Because most core node functions, and most third-party libraries in the wild do not try/catch their callback invocations.

The following code will throw an uncaughtException and there is no way to catch it. Even if core were to delegate this exception back to the callee this is potentially error-prone, as multiple callbacks have undefined behaviours.

So how do generators improve this? The following snippet introduces the same logic using generators and the Co library. You might think “this is just stupid syntax sugar” - but you would be incorrect. Since we pass the generator to the `co()` function, and all yields delegate to the caller, extremely robust error handling can be delegated to Co.

Libraries like Co can “throw” exceptions back to their origin as shown below, meaning you can use try/catch as the language intended, or leave them out and utilize the final Co callback to handle the error.

At the time of writing Co seems to be the only one which implements robust error handling, but if you look at the Co source you’ll notice all of the try/catch blocks. If you do not use generators you effectively have to inline all of these try/catch blocks into every library you’ve ever written to make it truly robust. This is what makes writing robust code with Node.js almost impossible as it is today.

Generators vs coroutines

Generators are sometimes referred to as “semicoroutines”, a more limited form of coroutine that may only yield to its caller. This makes the use of generators more explicit than coroutines, as only yielded values may suspend the “thread”.

Coroutines are more flexible in this respect, and looks just like regular blocking code as no yield is required:

Some people view full coroutines as “dangerous”, as it’s unclear which function does or does not suspend the thread. Personally I find this argument silly, most functions that suspend are pretty obvious, things like reading and writing to files or sockets, http requests, sleeps, and so on will not surprise anyone when they suspend execution of that thread.

If this is undesirable then you “fork” off and force the task to become asynchronous, much like you do in Go.

In my opinion generators are potentially more dangerous than coroutines (while still less so than callbacks) — as simply forgetting a yield expression could leave you puzzled or cause undefined behaviour when it executes after the rest of your code. Either way both semi— and full coroutines have different pros and cons, I’m happy that we at least have one.

Let’s look at how you can employ this new construct using generators.

Simplifying async flow control with coroutines

You’ve already seen how a simple read / write operation looks more elegant than the callback variant, but let’s look at some more patterns.

Assuming all operations execute sequentially by default simplifies the mental model, and while some people claim generators or coroutines complicate state, this is misinformation, working with state is identical to that of callbacks. Global variables remain global, local variables remain local, and closures remain closures.

To illustate flow let’s say as an example you wanted to request a web page’s html, parse the links, then request all those bodies in parallel and output their content-types.

Here’s how that might look using regular callbacks, without the use of any third-party flow control library.

Such a simple task quickly loses all meaning with callbacks. After adding in error delegation, double-callback prevention, storing of the results, and the callbacks themselves you really don’t even get an idea of what the intent of this function is. Oh and if you wanted to make this more robust you’d have to try/catch the final fn(null, results) as well.

Now here is the same showTypes() function implemented with generators. As you can see the resulting signature is identical to that of the callback implementation, both of these concepts may co-exist. In this example Co handles all of the mundane error handling and result stuffing that we had to do manually above. The array yielded via urls.map(get) is executed in parallel, however responses retain order.