Last active
June 17, 2016 20:38
-
-
Save faiwer/c8e18a7a92125ceaff8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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