Skip to content

Instantly share code, notes, and snippets.

@faiwer
Last active June 17, 2016 20:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faiwer/c8e18a7a92125ceaff8a to your computer and use it in GitHub Desktop.
Save faiwer/c8e18a7a92125ceaff8a to your computer and use it in GitHub Desktop.
(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