title: Proper way to use data returned by an asynchronous function (web scraping example)
body: <p>So I have this simple code to mine some videos url in order to apply another scraping function to it afterward. My problem is that I can't seem to return the url-filled array. I know that it's a problem of scope but I'm not that familiar with Javascript and my knowledge got me as far as I could. </p>
  
  <p>Here is the code : </p>
  
  <pre><code>var request = require('request');
  var cheerio = require('cheerio');
  
  var startUrl = 'http://www.somewebsite.com/mostviewed';
  
  var getVideoIds = function(url) {
  
      var urls = [];
  
      request(url, function(err, resp, body){
          if (err)
              throw err;
          $ = cheerio.load(body);
  
  
          var videoUrls = [];
          $('.videoTitle a').each(function() {
              videoUrls.push($(this).attr('href'));
          });
      });
  
     return urls;
  }
  
  
  var urlsToScrap = getVideoIds(startUrl);
  console.log(urlsToScrap);
  </code></pre>
  
  <p>PS : the current code returns an empty array;</p>