Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • Since forEach does not wait for each promise to resolve - What do you mean by this. That is the purpose of the await keyword. Does that mean that forEach returns a list a promises, const promise_list = forEach(async() => {some async call....}) Commented Sep 17, 2024 at 22:00
  • The await keyword works inside the async function, but forEach() continues executing the next iteration without waiting for the current one to finish. So even though you're using await, the forEach() function doesn't know what await is Commented Sep 18, 2024 at 0:54
  • forEach() is designed for synchronous operations. It doesn't handle asynchronous operations, which is why it doesn’t know or handle await properly. Commented Sep 18, 2024 at 0:57
  • javascript has microtask queue where promises are handled javascript.info/microtask-queue Commented Sep 18, 2024 at 1:08
  • so each promise inside forEach is passed to microtask queue. while promises are handled here, since forEach is treated as sync code, it keeps iterating. The Event Loop processes this microtask queue as soon as it finishes any synchronous tasks on the call stack. that is why if you check the code example, after forEach iteration is over, store array is empty, because promises from microtask has not been executed on call stack Commented Sep 18, 2024 at 1:13