如何在基于回调的循环中使用 yield?

How to use yield with callback based loops?

虽然 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.forEachArray.prototype.findArray.prototype.sort。所有这些都基于为每次迭代传递的回调。我什至听说有人建议我们尽可能使用这些,因为它们可以比标准 for 循环更好地优化。

我也经常使用基于回调的循环来抽象出一些复杂的循环模式。

这里的问题是,是否可以将它们变成基于 yield 的迭代器?举个简单的例子,假设我想让你对数组进行异步排序。

tl;dr: You can’t do that, but check out this other thing you can do with the latest V8 and 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-based 实现 – 例如,流行的承诺库,如 bluebird,已经为 mapreduce 执行此操作 – 和使用 async/await 而不是生成器(因为 async 函数 return promises):

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);
});