title: Concurrency in resolving an array of promises
body: <p>So basically I found that promises would resolve concurrently when you create an array of promises and deal with <strong>individual promises</strong> in a loop... But it doesn't work when you create individual promises and deal with them in a loop... </p>
  
  <p>So basically this question comes from me playing with axios and I originally thought this was a bug (<a href="https://github.com/axios/axios/issues/1973" rel="nofollow noreferrer">https://github.com/axios/axios/issues/1973</a>). However I soon discovered it was actually a problem with misunderstanding some part of Promise, especially <code>await Promise</code>. However I don't think I could find anything in the MDN document unless I have missed something.</p>
  
  <p>This will <strong>not work</strong>:</p>
  
  <pre class="lang-js prettyprint-override"><code>let results = [];
  for (let i=1; i&lt;=5; i++) {
      let promise = new Promise(function(resolve, reject) {
          setTimeout(function() {
              resolve('foo');
          }, 10000);
      })
      results.push(await promise);
  }
  </code></pre>
  
  <p>But this <strong>will</strong>: </p>
  
  <pre class="lang-js prettyprint-override"><code>let results = [];
  
  const promises = [
      new Promise(function(resolve, reject) {
          setTimeout(function() {
              resolve('foo');
          }, 10000);
      }),
      new Promise(function(resolve, reject) {
          setTimeout(function() {
              resolve('foo');
          }, 10000);
      }),
      new Promise(function(resolve, reject) {
          setTimeout(function() {
              resolve('foo');
          }, 10000);
      }),
      new Promise(function(resolve, reject) {
          setTimeout(function() {
              resolve('foo');
          }, 10000);
      }),
      new Promise(function(resolve, reject) {
          setTimeout(function() {
              resolve('foo');
          }, 10000);
      }),
  ];
  
  for (let i=1; i&lt;=5; i++) {
      results.push(await promises[i-1]);
  }
  </code></pre>
  
  <p>I would expect, in both cases, that the array of promises (<code>results</code>) to resolve in a concurrent manner. </p>
  
  <p>However in the first example, the promises actually resolve one after another.</p>