← Fullstack with Vue.jsQuestion 28 of 62
Why doesn't await work inside forEach()?
📘 Explanation
forEach() doesn't know about promises, so it just fires off all the callbacks without waiting for each await to finish, which is why a plain for...of loop with await is the fix. The other explanations are incorrect because forEach() doesn't convert async functions to sync, doesn't use separate threads, and await can be used inside any async function, including callbacks, not just at the top level.
Related questions
- What happens if you forget to use await before calling an async function?
- What does an async function return in JavaScript?
- Can you use await anywhere in JavaScript code?
- How does Vue's reactivity system work under the hood?
- What's the difference between watchEffect and watch(..., { deep: true }) in Vue 3?
← PreviousNo login needed