title: JS async/await populating array with links then downloading once array is populated
body: <p>I know it's been asked multiple times but I'm still unable to find a solution to my particular problem. Basically as per the title, I have written a function <code>getLinks()</code> that would populate a global array <code>links</code> through api calls from <a href="https://data.gov.sg/dataset/traffic-images?view_id=a0e54baf-6ef7-4bca-9aa2-8c6ca37a0a0e&amp;resource_id=e127e29a-bd48-47e2-a0a7-e89ce31f10c7" rel="nofollow noreferrer">SG Gov Traffic Data API</a>.</p>
  
  <p>The problem arises when I try to download the images via function <code>getImages()</code> as this function needs to wait for the <code>links</code> array to be populated before running. I can't seem to get the logic right for the sequential execution of the functions.</p>
  
  <p>Full code below. Thanks in advance for any pointers!</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var fs = require('fs'),
      request = require('request');
  
  // Download individual content
  function download(uri, filename, callback){
    request.head(uri, function(err, res, body){
      request(uri).pipe(fs.createWriteStream('training/' + filename)).on('close', callback);
    });
  };
  
  // Get image links from API
  var links =[];
  
  function two(n){
      return n &gt; 9 ? "" + n: "0" + n;
  }
  
  async function getLinks() {
    links = []; // Clear the links array
    for(i = 1; i &lt; 30; i++) { // Loop through 30 days, chosen month is Dec 2018
      for(j = 0; j &lt; 24; j++) { // Loop through 24 hours each day
        request('https://api.data.gov.sg/v1/transport/traffic-images?date_time=2018-12-' + two(i) + 'T' + two(j) + '%3A00%3A00', function(error, response, body) {
          if(!error &amp;&amp; response.statusCode == 200) {
            var parsedData = JSON.parse(body);
            links.push(parsedData.items[0]["cameras"][0]["image"].toString())
          } else {
            console.log(error)
          }
        })
      }
    }
    return;
  }
  
  // Scrape and download to directory
  async function getImages() {
    await getLinks();
    for( i = 0; i &lt; links.length; i++) {
      download(links[i], i, function(){});
    }
  }
  
  getImages();</code></pre>
  </div>
  </div>
  </p>