co-booster v1.0.2
co-booster
co-booster is a performance tuning version of co. If you're already using co, you can make it work more faster by changing only the module. Naturally, It's fully compatible with co.
co is Generator based control flow goodness for nodejs and the browser, using promises, letting you write non-blocking code in a nice-ish way. You can check out co here.
This work is all thanks to tj who is the author of co.
How to change from co
Just change the module. You don't need anything else.
//var co = require('co');
var co = require('co-booster');
co(function *(){
...
});Performance
co() - benchmark code
- Node 4.4.7 : 8% faster
- Node 6.9.1 : 12% faster
co.wrap() - benchmark code
- Node 4.4.7 : 3% faster
- Node 6.9.1 : 8% faster
koa@1 style - benchmark code
- Node 4.4.7 : 15% faster
- Node 6.9.1 : 32% faster
Platform Compatibility
co-booster 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-booster without flags or polyfills.
Installation
$ npm install co-boosterAssociated libraries
Any library that returns promises work well with co-booster.
- mz - wrap all of node's code libraries as promises.
View the wiki for more libraries.
Examples
var co = require('co-booster');
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
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-booster.
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.
co(function* () {
var res = yield {
1: Promise.resolve(1),
2: Promise.resolve(2),
};
console.log(res); // => { 1: 1, 2: 2 }
}).catch(onerror);Generators and Generator Functions
Any generator or generator function you can pass into co-booster
can be yielded as well. This should generally be avoided
as we should be moving towards spec-compliant Promises instead.
How to skip errors in unsupported objects
If you set skipUnsupportError, co will no longer throw errors for unsupported objects.
co.skipUnsupportError = true;
co(function* () {
yield undefined;
});API
co(fn*).then( val => )
Returns a promise that resolves a generator, generator function, or any function that returns a generator.
co(function* () {
return yield Promise.resolve(true);
}).then(function (val) {
console.log(val);
}, function (err) {
console.error(err.stack);
});var fn = co.wrap(fn*)
Convert a generator into a regular function that returns a Promise.
var fn = co.wrap(function* (val) {
return yield Promise.resolve(val);
});
fn(true).then(function (val) {
});License
MIT