title: Executing promises in sequence: understanding a claim made about this example code
body: <p>I'm not looking for a solution for how to execute promises in sequence, I'm trying to understand the one given by a blog author:</p>
  
  <p>I am reading <a href="http://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html" rel="nofollow">"We have a problem with promises"</a> by Nolan Lawson, posted 18 May 2015.</p>
  
  <p>Under "Advanced mistake #3: promises vs promise factories" he has this example that is supposed to be the working one:</p>
  
  <pre class="lang-js prettyprint-override"><code>// Setup, just a meta explanation what a function looks like
  function promiseFactory() {
    return somethingThatCreatesAPromise();
  }
  
  // Actual example; promiseFactories could be an Array
  function executeSequentially(promiseFactories) {
      var result = Promise.resolve();
      promiseFactories.forEach(function (promiseFactory) {
          result = result.then(promiseFactory);
      });
      return result;
  }
  </code></pre>
  
  <p>I don't understand why that works. <code>forEach</code> executes all functions at once. <code>result</code> will be a resolved promise so <code>promiseFactory</code> is run right away?</p>
  
  <p>I don't get how this code is supposed to ensure the code in the <code>promiseFactory</code> functions runs in sequence.</p>