DEV Community

Discussion on: Async/Await and the forEach Pit of Despair

Collapse
 
adebiyial profile image
Adebiyi Adedotun

The truth I learnt from experiment is in two parts:

According to the MDN docs,

  1. forEach() method executes a provided function once for each array element.
    Which doesn't mean sequentially (but in here, in parallel), but we are already used to executing things synchronously most of the times we never take async into consideration. So, practically, async has the right to change the order of execution, whatever promise gets the baton goes first...

  2. I also figured something with functions that require callbacks... again the forEach() MDN says: forEach() method executes a provided function once for each array element. "Once for each array element" And based on my experience with this, what we should really aim to iterate and perform an action upon is the array(posts.data) item, in this case "post" not "user" because that breaks the purpose of the forEach() method.