JavaScript · Middle · Question 21 of 75

Why doesn't forEach wait for async callbacks?

items.forEach(async (item) => { await save(item); });
console.log("done");

Explanation

The callback returns a promise, but forEach doesn't use it. You need for...of with await (sequential) or await Promise.all(items.map(save)) (parallel).

Further reading

Problem with a question

JavaScript · Middle · question 21

Test yourself

Take the JavaScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz