title: Async calls per each array element and wait for completion
body: <p>I found many similar questions but none with the same purpose (or maybe i just didnt understand? Can be that as well).</p>
  
  <p>I've an array of elements, a function A that performs an async operation with an element and returns a promise, and the function B i'm working on which should call A for each array element, and return a promise once all the A calls have been completed. How can i do that?
  Relevant code:</p>
  
  <pre><code>var img = [];
  img[0] = "http://www.something.com/picture0.jpg";
  img[1] = "http://www.something.com/picture1.jpg";
  img[2] = "http://www.something.com/picture2.jpg";
  function ask(img)
      {
      return new Promise((resolve, reject) =&gt;
          {
          if(something)
              {resolve("test");}
          else
              {reject("test");}
          }
      }
  function analyze_all(img)
      {
      for(var i in img)
          {
          ask(img[i])
          .then((res) =&gt; 
              {
              ???
              })
          .catch((err) =&gt;
              {
              ???
              });
          }
      return ???
      }
  </code></pre>
  
  <p>I thought about pushing all results to a list, like that:</p>
  
  <pre><code>function analyze_all(img)
      {
      var ret = [];
      for(var i in img)
          {
          ask(img[i])
          .then((res) =&gt; 
              {
              ret.push({image: img[i], data: res});
              })
          .catch((err) =&gt;
              {
              ret.push({image: img[i], error: err});
              });
          }
      return ???
      }
  </code></pre>
  
  <p>But then how do i know when will all the async calls be completed?
  Please help!</p>
  
  <hr>
  
  <p>Solved. As addition, if someone has similar needs and wants to have all the "return" values whether they're resolves or rejects, i suggest checking the following link:
  <a href="https://nmaggioni.xyz/2016/10/13/Avoiding-Promise-all-fail-fast-behavior/" rel="nofollow noreferrer">https://nmaggioni.xyz/2016/10/13/Avoiding-Promise-all-fail-fast-behavior/</a></p>