- Notifications
You must be signed in to change notification settings - Fork783
The ultimate generator based flow-control goodness for nodejs (supports thunks, promises, etc)
License
tj/co
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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 theasync/await proposal.The primary API change is howco() is invoked.Before,co returned a "thunk", which you then called with a callback and optional arguments.Now,co() returns a promise.
co(function*(){varresult=yieldPromise.resolve(true);returnresult;}).then(function(value){console.log(value);},function(err){console.error(err.stack);});
If you want to convert aco-generator-function into a regular function that returns a promise,you now useco.wrap(fn*).
varfn=co.wrap(function*(val){returnyieldPromise.resolve(val);});fn(true).then(function(val){});
co@4+ requires aPromise implementation.For versions of node< 0.11 and for many older browsers,you should/must include your ownPromise polyfill.
When using node 0.10.x and lower or browsers without generator support,you must usegnode and/orregenerator.
When using node 0.11.x, you must use the--harmony-generatorsflag or just--harmony to get access to generators.
Node v4+ is supported out of the box, you can useco without flags or polyfills.
$ npm install coAny library that returns promises work well withco.
- mz - wrap all of node's code libraries as promises.
View thewiki for more libraries.
varco=require('co');co(function*(){// yield any promisevarresult=yieldPromise.resolve(true);}).catch(onerror);co(function*(){// resolve multiple promises in parallelvara=Promise.resolve(1);varb=Promise.resolve(2);varc=Promise.resolve(3);varres=yield[a,b,c];console.log(res);// => [1, 2, 3]}).catch(onerror);// errors can be try/catchedco(function*(){try{yieldPromise.reject(newError('boom'));}catch(err){console.error(err.message);// "boom"}}).catch(onerror);functiononerror(err){// log any uncaught errors// co will not throw any errors you do not handle!!!// HANDLE ALL YOUR ERRORS!!!console.error(err.stack);}
Theyieldable objects currently supported are:
- promises
- thunks (functions)
- array (parallel execution)
- objects (parallel execution)
- generators (delegation)
- generator functions (delegation)
Nestedyieldable objects are supported, meaning you can nestpromises 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 maybe removed in future versions ofco.
yielding an array will resolve all theyieldables in parallel.
co(function*(){varres=yield[Promise.resolve(1),Promise.resolve(2),Promise.resolve(3),];console.log(res);// => [1, 2, 3]}).catch(onerror);
Just like arrays, objects resolve allyieldables in parallel.
co(function*(){varres=yield{1:Promise.resolve(1),2:Promise.resolve(2),};console.log(res);// => { 1: 1, 2: 2 }}).catch(onerror);
Any generator or generator function you can pass intococan be yielded as well. This should generally be avoidedas we should be moving towards spec-compliantPromises instead.
Returns a promise that resolves a generator, generator function,or any function that returns a generator.
co(function*(){returnyieldPromise.resolve(true);}).then(function(val){console.log(val);},function(err){console.error(err.stack);});
Convert a generator into a regular function that returns aPromise.
varfn=co.wrap(function*(val){returnyieldPromise.resolve(val);});fn(true).then(function(val){});
MIT
About
The ultimate generator based flow-control goodness for nodejs (supports thunks, promises, etc)
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.