title: What is the order of execution in JavaScript promises?
body: <p>I'd like to understand the execution order of the following snippet that uses JavaScript promises.</p>
  <pre><code>Promise.resolve('A')
    .then(function(a){console.log(2, a); return 'B';})
    .then(function(a){
       Promise.resolve('C')
         .then(function(a){console.log(7, a);})
         .then(function(a){console.log(8, a);});
       console.log(3, a);
       return a;})
    .then(function(a){
       Promise.resolve('D')
         .then(function(a){console.log(9, a);})
         .then(function(a){console.log(10, a);});
       console.log(4, a);})
    .then(function(a){
       console.log(5, a);});
  console.log(1);
  setTimeout(function(){console.log(6)},0);
  </code></pre>
  <p>The result is:</p>
  <pre class="lang-none prettyprint-override"><code>1
  2 &quot;A&quot;
  3 &quot;B&quot;
  7 &quot;C&quot;
  4 &quot;B&quot;
  8 undefined
  9 &quot;D&quot;
  5 undefined
  10 undefined
  6
  </code></pre>
  <p>I'm curious about the execution order 1 2 3 7... not the values <code>&quot;A&quot;</code>, <code>&quot;B&quot;</code>...</p>
  <p>My understanding is that if a promise is resolved the <code>then</code> function is put in the browser event queue. So my expectation was 1 2 3 4 ...</p>
  <p>Why isn't 1 2 3 4 ... the logged order?</p>