title: Waiting for loops to finish by using await on the loop
body: <p>Synchronicity in js loops is still driving me up the wall.</p>
  
  <p>What I want to do is fairly simple</p>
  
  <pre><code>async doAllTheThings(data, array) {
      await array.forEach(entry =&gt; {
          let val = //some algorithm using entry keys
          let subVal = someFunc(/*more entry keys*/)
          data[entry.Namekey] = `${val}/${subVal}`;
      });
      return data; //after data is modified
  }
  </code></pre>
  
  <p>But I can't tell if that's actually safe or not.  I simply don't like the simple loop pattern</p>
  
  <pre><code>for (i=0; i&lt;arrayLength; i++) {
      //do things
      if (i === arrayLength-1) {
          return
      }
  }
  </code></pre>
  
  <p>I wanted a better way to do it, but I can't tell if what I'm trying is working safely or not, or I simply haven't hit a data pattern that will trigger the race condition.</p>
  
  <p>Or perhaps I'm overthinking it.  The algorithm in the array consists solely of some MATH and assignment statements...and a small function call that itself also consists solely of more MATH and assignment statements.  Those are supposedly fully synchronous across the board.  But loops are weird sometimes.</p>
  
  <p><strong>The Question</strong></p>
  
  <p>Can you use await in that manner, outside the loop itself, to trigger the code to wait for the loop to complete?  Or is the only safe way to accomplish this the older manner of simply checking where you are in the loop, and not returning until you hit the end, manually.</p>