Last active
August 29, 2015 14:18
-
-
Save dickeylth/c210ceb23bc543f092bf to your computer and use it in GitHub Desktop.
es6 co generator
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
// demo #1 | |
var co = require('co'); | |
function printTime() { | |
"use strict"; | |
return '[' + Date.now() + '] '; | |
} | |
co(function* () { | |
"use strict"; | |
var a = new Promise(function (resolve, reject) { | |
setTimeout(function () { | |
console.log(printTime() + 'a done. '); | |
resolve('abc'); | |
}, 1000); | |
}); | |
var b = new Promise(function (resolve, reject) { | |
setTimeout(function () { | |
console.log(printTime() + 'b done. '); | |
resolve('def'); | |
}, 2000); | |
}); | |
var c = new Promise(function (resolve, reject) { | |
setTimeout(function () { | |
console.log(printTime() + 'c done. '); | |
resolve('pkq'); | |
}, 3000); | |
}); | |
console.log(printTime() + 'start...'); | |
var aValue = yield a; | |
console.log(printTime() + 'a yielded' + ' - ' + aValue); | |
var bValue = yield b; | |
console.log(printTime() + 'b yielded' + ' - ' + bValue); | |
var cValue = yield c; | |
console.log(printTime() + 'c yielded' + ' - ' + cValue); | |
}) | |
.then(function () { | |
"use strict"; | |
console.log(printTime() + 'co end'); | |
}) | |
.catch(function (e) { | |
"use strict"; | |
console.error(e); | |
}); | |
/* demo #1 result: | |
/usr/local/bin/node --harmony co-test.js | |
[1428654896938] start... | |
[1428654897929] a done. | |
[1428654897931] a yielded - abc | |
[1428654898928] b done. | |
[1428654898928] b yielded - def | |
[1428654899928] c done. | |
[1428654899928] c yielded - pkq | |
[1428654899929] co end | |
Process finished with exit code 0 | |
*/ | |
// demo #2,把 a 和 c 的延时时长对调 | |
var co = require('co'); | |
function printTime() { | |
"use strict"; | |
return '[' + Date.now() + '] '; | |
} | |
co(function* () { | |
"use strict"; | |
var a = new Promise(function (resolve, reject) { | |
setTimeout(function () { | |
console.log(printTime() + 'a done. '); | |
resolve('abc'); | |
}, 3000); | |
}); | |
var b = new Promise(function (resolve, reject) { | |
setTimeout(function () { | |
console.log(printTime() + 'b done. '); | |
resolve('def'); | |
}, 2000); | |
}); | |
var c = new Promise(function (resolve, reject) { | |
setTimeout(function () { | |
console.log(printTime() + 'c done. '); | |
resolve('pkq'); | |
}, 1000); | |
}); | |
console.log(printTime() + 'start...'); | |
var aValue = yield a; | |
console.log(printTime() + 'a yielded' + ' - ' + aValue); | |
var bValue = yield b; | |
console.log(printTime() + 'b yielded' + ' - ' + bValue); | |
var cValue = yield c; | |
console.log(printTime() + 'c yielded' + ' - ' + cValue); | |
}) | |
.then(function () { | |
"use strict"; | |
console.log(printTime() + 'co end'); | |
}) | |
.catch(function (e) { | |
"use strict"; | |
console.error(e); | |
}); | |
/* | |
/usr/local/bin/node --harmony co-test.js | |
[1428654987498] start... | |
[1428654988482] c done. | |
[1428654989480] b done. | |
[1428654990478] a done. | |
[1428654990478] a yielded - abc | |
[1428654990478] b yielded - def | |
[1428654990478] c yielded - pkq | |
[1428654990479] co end | |
Process finished with exit code 0 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
貌似是被 console 给骗了。。。debug 跟着执行可以看出 demo #2 实际上的执行流: