即使等待在循环中,我似乎也无法弄清循环为什么全速执行。
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);
}