title: nodejs execute in order using foreach vs for
body: <p>I wanna execute series of things in the same order for each item i have and collect the information then send statistics at the end.</p>
  
  <p>here is the example code:</p>
  
  <pre><code>let listOfItems = [];
  assignedItems.forEach(async (item) =&gt; {
      let id = item.myID;
      totalItemCount += await itemRepository.getItemCount(id);
  
      let itemDetail = await itemRepository.getDetails(id);
      listOfItems.push(item.name);
  }, this);
  console.log(listOfItems.length); // 0 ??
  </code></pre>
  
  <p>vs</p>
  
  <pre><code>let listOfItems = [];
  for (let i = 0; i &lt; assignedItems.length; i++) {
      let id = assignedItems[i].myID;
      totalItemCount += await itemRepository.getItemCount(id);
  
      let itemDetail = await itemRepository.getDetails(id);
      listOfItems.push(itemDetail.name);
  }
  console.log(listOfItems.length); // 5 as expected
  </code></pre>
  
  <p>output using foreach: 0 (because foreach is executed later)
  output using for: 5 as expected (because for is executed as code execution is written)</p>
  
  <p>Main issue is: foreach is executed after my console.logs which are written after foreach as i want to collect first all the details for the items and then send the data i have collected.</p>
  
  <p>What the hell is doing on? how do i fix the issue using foreach?</p>