Co
Generator based flow-control goodness for nodejs and the browser, using thunks or promises, letting you write non-blocking code in a nice-ish way.
Co is careful to relay any errors that occur back to the generator, including those
within the thunk, or from the thunk's callback. "Uncaught" exceptions in the generator
are passed to co()
's thunk.
Make sure to view the examples.
Platform Compatibility
When using node 0.11.x or greater, you must use the --harmony-generators
flag or just --harmony
to get access to generators.
When using node 0.10.x and lower or browsers without generator support, you must use gnode and/or regenerator.
When using node 0.8.x and lower or browsers without setImmediate
,
you must include a setImmediate
polyfill.
For a really simple polyfill, you may use component/setimmediate.js.
For a more robust polyfill, you may use YuzuJS/setImmediate.
Installation
$ npm install co
Associated libraries
View the wiki for libraries that work well with Co.
Example
var co = ;var thunkify = ;var request = ;var get = ; // Error handling
Yieldables
The "yieldable" objects currently supported are:
- promises
- thunks (functions)
- array (parallel execution)
- objects (parallel execution)
- generators (delegation)
- generator functions (delegation)
To convert a regular node function that accepts a callback into one which returns a thunk you may want to use thunkify or similar.
Thunks vs promises
While co supports promises, you may return "thunks" from your functions,
which otherwise behaves just like the traditional node-style callback
with a signature of: (err, result)
.
For example take fs.readFile
, we all know the signature is:
fs;
To work with Co we need a function to return another function of the same signature:
fs{ };
Which basically looks like this:
{ return { fs; }}
or to execute immediately like this (see thunkify
):
{ // call fs.readFile immediately, store result later return { // cb(err, result) or when result ready }}
Receiver propagation
When co
is invoked with a receiver it will propagate to most yieldables,
allowing you to alter this
.
var ctx = {}; { ;}
You also pass arguments through the generator:
;
API
co(fn)
Pass a generator fn
and return a thunk. The thunk's signature is
(err, result)
, where result
is the value passed to the return
statement.
var co = ;var fs = ; { return { fs; }}
You may also yield Generator
objects to support nesting:
var co = ;var fs = ; { return { fs; }} { var a = ; var b = ; var c = ; return a b c;} { var a = ; var b = ; var c = ; return a b c;}
Or if the generator functions do not require arguments, simply yield
the function:
var co = ;var thunkify = ;var request = ; var get = ; { var a = var b = var c = return a b c}
If a thunk is written to execute immediately you may achieve parallelism
by simply yield
-ing after the call. The following are equivalent if
each call kicks off execution immediately:
Or:
You can also pass arguments into the generator. The last argument, done
, is
the callback function. Here's an example:
var exec = ;'pwd' done;
yield array
By yielding an array of thunks you may "join" them all into a single thunk which executes them all concurrently, instead of in sequence. Note that the resulting array ordering is retained.
var co = ;var fs = ; { return { fs; }}
Nested arrays may also be expressed as simple nested arrays:
var a = ; var b = ; console;
yield object
Yielding an object behaves much like yielding an array, however recursion is supported:
Here is the sequential equivalent without yielding an object:
Performance
On my machine 30,000 sequential stat()s takes an avg of 570ms,
while the same number of sequential stat()s with co()
takes
610ms, aka the overhead introduced by generators is extremely negligible.
License
MIT