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 | |
*/ |
貌似是被 console 给骗了。。。debug 跟着执行可以看出 demo #2 实际上的执行流:
/usr/local/bin/node --debug-brk=53885 --nolazy --harmony co-test.js
Debugger listening on port 53885
[1428656786806] start...
[1428656786809] a done.
[1428656787820] a yielded - abc
[1428656791926] b done.
[1428656792936] b yielded - def
[1428656795012] c done.
[1428656796025] c yielded - pkq
[1428656797039] co end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
demo #1 中,a, b, c 3 个 Promise 看上去完美滴“同步阻塞”地执行了;
demo #2 中,为何是 a、b、c 3 个 Promise 执行完之后才执行三条
console.log(printTime() + '...')
语句呢?