A little over a year ago, I added 'await' support to node.js (Chrome V8). I had also planned on adding support for 'yield', but never got around to it. But last night, I needed a break from another project I'd been working on, and decided to try my hand at implementing 'yield'.
A quick summary: await is a syntax/runtime feature from C# 5.0 that allows suspension execution of a function until a task (ie, a callback) is completed. The function does not block.
Here's an example of code without await. I want to download two text files in node.js, combine them, and return them to the caller:
Om nom nom, callback spaghetti!
What's this same function look like with 'await'?
Keep in mind, that 'await' does not block. It suspends execution, by turning everything below the await into the callback body. The 'await' is syntactic sugar. The two examples are essentially equivalent. You can think of 'await' as an asynchronous 'var' declaration.
The underpinnings of yield and await are essentially the same. They both, under the hood, are leveraging 'call/cc', short for call-with-continuation. The call/cc mechanism is what allows mid-function execution suspension and resume.
Generators are extremely powerful. They allow a program to create an iterator that evaluates the next result upon every iteration. Consider the following program written without generators. The entire fibonacci series (up to 10) is generated and returned up front:
In this generator example, note that the 'while' loop seems as if the program would continue infinitely... but it does not due to the 'yield' which suspends execution until the next iteration. The fibonacci series is generated on an as needed basis.
Chances of this being taken upstream are minimal. I imagine the V8 guys will eventually get around to doing their own proper implementation. Admittedly, my understanding of V8 internals and optimization is naive. And the node.js guys will want to remain on V8 proper. So, in the meantime, I'll continue maintaining this fork.
If you're a CoffeeScript fan and want similar functionality, you may want to check out IcedCoffeeScript. It brings async/defer to CoffeeScript.
My modified version of node.js/V8 is on Github. Make sure you check out the async-v0.10.x branch.