title: Wait for asynchronous calls to complete and return
body: <p>I need to write some Cloud Functions in nodejs. This particular Cloud Function needs to iterate over an array of URLs and make some asynchronous calls to a third-party API.</p>
  
  <p>Here is some placeholder code that simulates what I'm trying to do, adapted from <a href="https://stackoverflow.com/a/50430721/3784008">here</a>:</p>
  
  <pre><code>function checkPhotos(photos) {
    var checkedPhotos = [];
    var promiseArray = photos.map(asyncFunction);
  
    Promise.all(promiseArray).then(results =&gt; {
      console.log('results are: ', results)
      results.forEach(result =&gt; { checkedPhotos.push(result) });
    })
  
    // I want to block here until the Promise.all call above completes
    // but it does not block, it returns [] immediately while the async
    // calls run
    return checkedPhotos;
  }
  
  // This is a pretend async function to simulate the third party API
  function asyncFunction(photo) {
    return new Promise((resolve, reject) =&gt; {
      setTimeout(() =&gt; {
        console.log("in async function, photo is " + photo)
        resolve(photo)
      }, Math.random()*2000)
    })
  }
  </code></pre>
  
  <p>Then I call the functions:</p>
  
  <pre><code>var photos = ['https://example.com/example.jpg', 'https://example.com/example2.jpg'];
  var checkedPhotos = checkPhotos(photos);
  </code></pre>
  
  <p>I expect <code>checkedPhotos</code> to contain the values from <code>results</code> but it is an empty array at first, then the asynchronous calls complete and the array gets populated. How do I make <code>checkPhotos()</code> block until all the asynchronous calls are complete so I can return <code>checkedPhotos</code> as a fully populated array?</p>