generator async control flow goodness
Generator based control flow goodness for nodejs and the browser, using promises, letting you write non-blocking code in a nice-ish way.
co@4.0.0 has been released, which now relies on promises.
It is a stepping stone towards ES7 async/await.
The primary API change is how co() is invoked.
Before, co returned a "thunk", which you then called with a callback and optional arguments.
Now, co() returns a promise.
co var result = yield Promiseresolvetrue; return result;then console.logvalue; console.errorerrstack;;
If you want to convert a co-generator-function into a regular function that returns a promise,
you now use co.wrap(fn*).
var fn = cowrap return yield Promiseresolveval;; fntruethen ;
co@4+ requires a Promise implementation.
For versions of node < 0.11 and for many older browsers,
you should/must include your own Promise polyfill.
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.
io.js is supported out of the box, you can use co without flags or polyfills.
$ npm install co
Any library that returns promises work well with co.
View the wiki for more libraries.
var co = require'co'; co // yield any promise var result = yield Promiseresolvetrue;catchonerror; co // resolve multiple promises in parallel var a = Promiseresolve1; var b = Promiseresolve2; var c = Promiseresolve3; var res = yield a b c; console.logres; // => [1, 2, 3] catchonerror; // errors can be try/catched co try yield Promisereject'boom'; catch err console.errorerrmessage; // "boom" catchonerror; // log any uncaught errors // co will not throw any errors you do not handle!!! // HANDLE ALL YOUR ERRORS!!! console.errorerrstack;
The yieldable objects currently supported are:
Nested yieldable objects are supported, meaning you can nest
promises within objects within arrays, and so on!
Thunks are functions that only have a single argument, a callback.
Thunk support only remains for backwards compatibility and may
be removed in future versions of co.
yielding an array will resolve all the yieldables in parallel.
co var res = yield Promiseresolve1 Promiseresolve2 Promiseresolve3 ; console.logres; // => [1, 2, 3] catchonerror;
Just like arrays, objects resolve all yieldables in parallel.
co var res = yield 1: Promiseresolve1 2: Promiseresolve2 ; console.logres; // => { 1: 1, 2: 2 } catchonerror;
Any generator or generator function you can pass into co
can be yielded as well. This should generally be avoided
as we should be moving towards spec-compliant Promises instead.
Returns a promise that resolves a generator, generator function, or any function that returns a generator.
co return yield Promiseresolvetrue;then console.logval; console.errorerrstack;;
Convert a generator into a regular function that returns a Promise.
var fn = cowrap return yield Promiseresolveval;; fntruethen ;
MIT