虽然 yield 关键字的主要目的是提供对某些数据的迭代器,但使用它来创建异步循环也相当方便:
function* bigLoop() {
// Some nested loops
for( ... ) {
for( ... ) {
// Yields current progress, eg. when parsing file
// or processing an image
yield percentCompleted;
}
}
}
然后可以异步调用:
function big_loop_async(delay) {
var iterator = big_loop();
function doNext() {
var next = iterator.next();
var percent_done = next.done?100:next.value;
console.log(percent_done, " % done.");
// start next iteration after delay, allowing other events to be processed
if(!next.done)
setTimeout(doNext, delay);
}
setTimeout(doNext, delay);
}
然而,在现代 javascript 中,基于回调的循环已经变得相当流行。我们有
Array.prototype.forEach
、Array.prototype.find
或Array.prototype.sort
。所有这些都基于为每次迭代传递的回调。我什至听说有人建议我们尽可能使用这些,因为它们可以比标准 for 循环更好地优化。
我也经常使用基于回调的循环来抽象出一些复杂的循环模式。
这里的问题是,是否有可能将它们变成基于
yield
的迭代器?举个简单的例子,假设我想让你对数组进行异步排序。
不,不可能让tl;dr:你不能那样做,但是看看你可以用最新的 V8 和 bluebird 做的另一件事:
async function asyncReduce() { const sum = await Promise.reduce( [1, 2, 3, 4, 5], async (m, n) => m + await Promise.delay(200, n), 0 ); console.log(sum); }
Array.prototype.sort
从它的比较函数中异步接受比较结果;您将不得不完全重新实现它。对于其他个别情况,可能会有 hack,比如协程
forEach
(它甚至不一定像你期望的那样工作,因为每个生成器都会运行到它的第一个
yield
,然后一个从
yield
继续) :
function syncForEach() {
[1, 2, 3, 4, 5].forEach(function (x) {
console.log(x);
});
}
function delayed(x) {
return new Promise(resolve => {
setTimeout(() => resolve(x), Math.random() * 1000 | 0);
});
}
function* chain(iterators) {
for (const it of iterators) {
yield* it;
}
}
function* asyncForEach() {
yield* chain(
[1, 2, 3, 4, 5].map(function* (x) {
console.log(yield delayed(x));
})
);
}
和
reduce
,它天生就很好用(直到你看性能):
function syncReduce() {
const sum = [1, 2, 3, 4, 5].reduce(function (m, n) {
return m + n;
}, 0);
console.log(sum);
}
function* asyncReduce() {
const sum = yield* [1, 2, 3, 4, 5].reduce(function* (m, n) {
return (yield* m) + (yield delayed(n));
}, function* () { return 0; }());
console.log(sum);
}
但是,是的,没有适用于所有功能的魔杖。
理想情况下,您将为所有这些功能添加基于 promise 的替代实现——例如,流行的 promise 库,如 bluebird,已经为
map
和
reduce
执行此操作——并使用
async
/
await
而不是生成器(因为
async
函数返回承诺):
async function asyncReduce() {
const sum = await Promise.reduce(
[1, 2, 3, 4, 5],
async (m, n) => m + await delayed(n),
0
);
console.log(sum);
}
如果 ECMAScript 有像 Python 这样理智的装饰器,你也不需要等待
async
支持来做这件事:
@Promise.coroutine
function* add(m, n) {
return m + (yield delayed(n));
}
@Promise.coroutine
function* asyncReduce() {
const sum = yield Promise.reduce([1, 2, 3, 4, 5], add, 0);
console.log(sum);
}
……但事实并非如此,所以你做到了。或者你可以接受这样的代码:
const asyncReduce = Promise.coroutine(function* () {
const sum = yield Promise.reduce([1, 2, 3, 4, 5], Promise.coroutine(function* (m, n) {
return m + (yield delayed(n));
}), 0);
console.log(sum);
});
const EventEmitter = require('events');
const eventEmitter = new EventEmitter();
eventEmitter.on('eachOne', number => {
console.log(`${number}`);
});
[1,3,5].forEach(i=>{
eventEmitter.emit('eachOne', i);
})