puzzle 11 · javascript
What does this log?
const ids = [1, 2, 3];
async function run() {
ids.forEach(async (id) => await work(id));
console.log("done");
}"done" after all work finishes
"done" immediately, before any work finishes
A TypeError
Nothing — it hangs
why
forEach ignores the promises its callback returns, so there is nothing to await. Use a for...of loop for sequential work, or await Promise.all(ids.map(work)) for parallel.