甚至在等待时也执行循环

时间:2019-03-11 08:20:17

标签: node.js

即使等待在循环中,我似乎也无法弄清循环为什么全速执行。

   model.collection.distinct("symbol", async function (error, distSymbols) {
        // iterate over the unique symbols and compute aggregate
        for (const symbol of distSymbols) {
            //
            //await computeAggregate(model, symbol);
            await dummy(Math.random() * 1000);
            console.log(symbol); //This will execute before any of the dummy awaits
        }
        res.json("success");
    });
});

const dummy = async(timeToWait) => {
    setTimeout(() => {
        console.log("I am waiting for", timeToWait);
        return true;
    }, timeToWait);
}

0 个答案:

没有答案