tj/co is an open-source project on GitHub with 11.8k stars, written primarily in JavaScript. The ultimate generator based flow-control goodness for nodejs (supports thunks, promises, etc)
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
Generator based control flow goodness for nodejs and the browser,
using promises, letting you write non-blocking code in a nice-ish way.
Co v4
[email protected] has been released, which now relies on promises.
It is a stepping stone towards the async/await proposal.
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(function* () {
var result = yield Promise.resolve(true);
return result;
}).then(function (value) {
console.log(value);
}, function (err) {
console.error(err.stack);
});
If you want to convert a co-generator-function into a regular function that returns a promise,
you now use co.wrap(fn*).
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.10.x and lower or browsers without generator support,
you must use gnode and/or regenerator.
When using node 0.11.x, you must use the --harmony-generators
flag or just --harmony to get access to generators.
Node v4+ is supported out of the box, you can use co without flags or polyfills.
Installation
$ npm install co
Associated libraries
Any library that returns promises work well with co.
mz - wrap all of node's code libraries as promises.
View the wiki for more libraries.
Examples
var co = require('co');
co(function *(){
// yield any promise
var result = yield Promise.resolve(true);
}).catch(onerror);
co(function *(){
// resolve multiple promises in parallel
var a = Promise.resolve(1);
var b = Promise.resolve(2);
var c = Promise.resolve(3);
var res = yield [a, b, c];
console.log(res);
// => [1, 2, 3]
}).catch(onerror);
// errors can be try/catched
co(function *(){
try {
yield Promise.reject(new Error('boom'));
} catch (err) {
console.error(err.message); // "boom"
}
}).catch(onerror);
function onerror(err) {
// log any uncaught errors
// co will not throw any errors you do not handle!!!
// HANDLE ALL YOUR ERRORS!!!
console.error(err.stack);
}
Yieldables
The yieldable objects currently supported are:
promises
thunks (functions)
array (parallel execution)
objects (parallel execution)
generators (delegation)
generator functions (delegation)
Nested yieldable objects are supported, meaning you can nest
promises within objects within arrays, and so on!
Promises
Read more on promises!
Thunks
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.
Arrays
yielding an array will resolve all the yieldables in parallel.
co(function* () {
var res = yield [
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
];
console.log(res); // => [1, 2, 3]
}).catch(onerror);
Objects
Just like arrays, objects resolve all yieldables in parallel.
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.
API
co(fn*).then( val => )
Returns a promise that resolves a generator, generator function,
or any function that returns a generator.
TopGit's last sync did not record any GitHub topics for tj/co. GitHub topics appear in the right sidebar of a repository page; that's the authoritative place to check.
How active is development on tj/co?
The most recent commit recorded on tj/co was 5.9 years ago, based on the GitHub push timestamp. The repository has 778 forks — one of the better signals of community interest.
How many stars does tj/co have?
tj/co has 11.8k GitHub stars — refresh the page for the live number, or check github.com/tj/co. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
What language is tj/co written in?
tj/co is written primarily in JavaScript. GitHub's language field is based on the largest share of bytes in the default branch.
Where do I read more about tj/co?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/tj/co is the definitive source.
Read full README in the tab above.
Want a second opinion on co?
Ask an AI that can read this page — one click and you get its take on co.