title: NodeJS: How to wait for a for-loop of async requests to complete before proceeding with code?
body: <p>I am relatively new to node and trying to create an web app that allows users to get retrieve information from the youtube api by providing a list of youtube channel IDs(already in server) by going to my <code>/retrieve</code> route. </p>
  
  <p>Currently I am looping through an array of objects with channel IDs, and for each ID I use a <code>setTimeOut</code> function to send 1request/500ms (due limitations of the youtube API) and using the <code>request.get</code> request module. I am trying to continue code and <code>res.send</code> AFTER all the requests to youtube is completed. Furtherfore, for each data obj I received from each youtube request, I am parsing and pushing them to the <code>youtubeinfo</code> array. </p>
  
  <p>I have a nodeJS version of v12.16.3.</p>
  
  <p>Here's my code below, sorry for the messiness:</p>
  
  <pre><code>
  var youtubeinfo = []
  
  router.get('/retrieve', async function(req, res, next) {
  
    const length = datajs.length
    console.log(length);
    var promiseArray = [];
  
      for(i = 0; i &lt; length; i++){
      promiseArray.push(new Promise((resolve, reject) =&gt; {
  
      (function(i){
          setTimeout(function(){
            const channelID = listOfID[i]['Channel ID']
            const url = youtubeAPIChannelID.concat(apiKey)
  
            request.get(url, (err, response, data)=&gt;{
              if (!err) {
  
                parsedData = JSON.parse(data)
                if (parsedData.items){
  
                  const parsedYoutubeobj = {
                  "custom_collection": {
                    "title": parsedData.items[0].brandingSettings.channel.title,
                    "body_html": parsedData.items[0].brandingSettings.channel.description,
                    "image": {
                      "src": parsedData.items[0].brandingSettings.image.bannerImageUrl,
                      "alt": "Rails Logo"
                    }
                  }
                }
                youtubeinfo.push(parsedYoutubeobj)
                console.log("----------------------")
  
                console.log(parsedYoutubeobj);
                youtubeinfo.push(parsedYoutubeobj);
                resolve()
                } else {
                  console.log("something is wrong...check if youtube channel still exist")
                  reject(error)
                }
  
              } else{
                console.log("statusCode: "+ response.statusCode)
                console.log("err: "+ err);
                reject(error)
  
              }
            })
        }, 500 * i);
      }(i));
    }))
    }
    res.send(await Promise.all(promiseArray));
  });
  </code></pre>
  
  <p>How can I correctly apply <code>promises/async/await</code> to have my code continue after all the async requests are made?
  I figured that using promises here would a solution. How do I edit <code>res.send(await Promise.all(promiseArray));</code> to send a string after all promise in promiseArray resolves?
  Many thanks in advance. </p>