title: Executing a callback function after .forEach finishes
body: <p>I'm trying to execute a function after a forEach loop has completed all iterations.</p>
  
  <p>This <a href="https://stackoverflow.com/a/18983245/3976696">answer</a> provides an interesting solution, but I can't get it to work.</p>
  
  <p>Here's the code I adapted, creating a simple asyncFunction(). </p>
  
  <pre><code>function callback () { console.log('all done'); }
  function asyncFunction(item) {
    console.log("in async function, item is " + item)
  }
  var itemsProcessed = 0;
  
  [1, 2, 3].forEach((item, index, array) =&gt; {
    asyncFunction(item, () =&gt; {
      itemsProcessed++;
      console.log("in callback area, itemsProcessed is " + itemsProcessed )
      if(itemsProcessed === array.length) {
        callback();
      }
    });
  });
  </code></pre>
  
  <p>As visible in this <a href="https://jsfiddle.net/xh1kL87a/1/" rel="nofollow noreferrer">JSfiddle</a>, the script correctly executes the async function, but fails to enter the part which increments <code>itemsProcessed</code> and should trigger the <code>callback()</code> function.</p>
  
  <p>I'm not too familiar with the fat arrow functions, so maybe the error comes from their usage. </p>
  
  <p>Can anyone explain why the script isn't behaving as expected?</p>