3

我一直在阅读有关如何不阻止 Node 的事件循环的信息。避免阻塞的一种方法是使用分区

我试图在我的代码中使用分区循环,但我似乎无法等待我的循环。这是我的代码的简化版本:

    const report = {
        someValue: 0
    };

    const runLoop = async () => {
        report.someValue += 1;

        // all sorts of async operations here that use async-await

        if (report.someValue < 1000) {
            await setImmediate(runLoop);
        }
    };

    await runLoop();
    console.log('Report is', report);

这将返回“Report is { someValue: 1 }”,但我希望 someValue 为 1000。

我猜 setImmediate 不会返回承诺,所以我尝试过承诺:

    const setImmediatePromise = util.promisify(setImmediate);

    const report = {
        someValue: 0
    };

    const runLoop = async () => {
        report.someValue += 1;

        // all sorts of async operations here that use async-await

        if (report.someValue < 1000) {
            await setImmediatePromise(runLoop);
        }
    };

    await runLoop();
    console.log('Report is', report);

但这也会返回“Report is { someValue: 1 }”。

那么,我怎样才能等待这个递归 setImmediate “循环”,以便我仅在整个递归周期完成后才进行 console.log 报告?

4

1 回答 1

4

当你有 promisified 时setImmediate,你不再向它传递回调。相反,你只是await它返回的承诺。然后你会做递归调用:

async function runLoop() {
    …
    if (…) {
        await setImmediatePromise();
        return runLoop();
    }
}

但是,async/await使您能够编写一个实际的循环:

const setImmediatePromise = util.promisify(setImmediate);

const report = {
    someValue: 0
};

while (report.someValue < 1000) {
    report.someValue += 1;
    // all sorts of synchronous operations here
    await setImmediatePromise();
}

console.log('Report is', report);

(注意递归的细微差别:在第一次迭代之前已经检查了条件,并且setImmediate在最后一次迭代之后再次运行。如果需要,使用do/while甚至while(true)+ if(…)break;。)

顺便说一句,如果您已经在循环内执行异步(非阻塞)操作,则没有理由添加额外setImmediate的操作。该指南仅处理会阻塞事件循环的复杂同步计算。

于 2020-06-15T09:48:32.980 回答