global.Promise = require('bluebird');
const co = Promise.coroutine;
const a = co(function* (abc) {
console.time(abc);
for (var i = 1; i < 10000; i++)
yield Promise.delay(0);
console.timeEnd(abc);
});
a('test1');
const b = async function(abc) {
console.time(abc);
for (var i = 1; i < 10000; i++)
await Promise.delay(0);
console.timeEnd(abc);
}
b('test2');
运行结果
test1: 13575.528ms
test2: 13819.517ms
速度基本一样,代码长度也基本一样,不过bluebird的corutine可以yield promise, 也可以yield generator function 没看出来 async await 的语法有什么优势?