繁体   English   中英

如何使用基于回调的循环的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 所有这些都基于每次迭代传递的回调。 我甚至听说建议我们尽可能使用这些,因为它们可以比循环的标准更好地进行优化。

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

这里的问题是,是否有可能将这些转化为基于yield的迭代器? 举个简单的例子,我想让你以异步方式对数组进行排序。

tl;博士:你不能这样做,但看看你可以用最新的V8和蓝鸟做的其他事情:

 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从其比较函数异步接受比较结果; 你必须完全重新实现它。 对于其他个别情况,可能存在黑客攻击,例如一个coroutiney 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,已经为mapreduce执行此操作,例如 - 并使用async / await而不是generator(因为async函数返回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);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM