Skip to content

Instantly share code, notes, and snippets.

@faiwer

faiwer/bluebird_vs_co.js

Last active Jun 17, 2016
Embed
What would you like to do?
(function()
{
"use strict";
function test(label, co)
{
let times = 3000;
function * g1(){ return 1; }
function * g2()
{
let val = yield g1();
return 2 + val;
}
function * gMain()
{
// require('./bad.js'); // syntax error
let val = 3;
for(let i = 0; i < times; ++ i)
val += yield g2();
return val;
};
console.time(label);
return co(gMain)
.then(val =>
{
console.log(`${label} result = ${val}`);
console.timeEnd(label);
})
.catch(err =>
{
console.timeEnd(label);
console.log(err.message);
console.log(err.stack);
});
}
let co = require('co');
let bluebird = require("bluebird");
let manual = require('bluebird-co/manual');
bluebird.coroutine.addYieldHandler(manual.toPromise);
let bluebirdCo = gen => bluebird.coroutine(gen)();
Promise.resolve(true)
.then(() => test('co', co))
.then(() => test('bluebird', bluebirdCo))
.then(() => console.log('… finished'))
.catch(e => console.log(e.message, e.stack));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment