title: await loop vs Promise.all
body: <p>Having a set of async operations on db to do, I'm wondering what's the difference performance-wise of doing a "blocking" <code>await</code> loop versus a <code>Promise.all</code>.</p>
  
  <pre><code>let insert = (id,value) =&gt; {
      return new Promise(function (resolve, reject) {
          connnection.query(`insert into items (id,value) VALUES (${id},"${value}")`, function (err, result) {
              if (err) return reject(err)
                  return resolve(result);
          });
      });
  };
  </code></pre>
  
  <p>Promise.all solution (it needs a for loop to builds the array of promises..)</p>
  
  <pre><code>let inserts = [];
  for (let i = 0; i &lt; SIZE; i++) inserts.push(insert(i,"..string.."))
  Promise.all(inserts).then(values =&gt; { 
      console.log("promise all ends");
  });
  </code></pre>
  
  <p>await loop solution</p>
  
  <pre><code>let inserts = [];
  (async function loop() {
      for (let i = 0; i &lt; SIZE; i++) {
          await insert(i, "..string..")
      }
      console.log("await loop ends");
  })
  </code></pre>
  
  <p>Edit: thanks for the anwsers, but I would dig into this a little more.
  <code>await</code> is not really blocking, we all know that, it's blocking in its own code block. An <code>await</code> loop sequentially fire requests, so if in the middle 1 requests takes longer, the other ones waits for it.
  Well this is similar to <code>Promise.all</code>: if a 1 req takes longer, the callback is not executed until ALL the responses are returned.</p>