title: Node.js wait until request is done
body: <p>I'm trying to make a simple app that requests some data from an API. I'm building it with express and I have the following code that does the request:</p>
  
  <pre><code>module.exports = {
      getPrevMatches: function(){
          request({
              uri: 'http://worldcup.sfg.io/matches',
              json: true
          }, this.process);
      },
      process: function(error, response, body){
          if(response.statusCode === 200){
              return body;
          }
      }
  }
  </code></pre>
  
  <p>And the following in my express script:</p>
  
  <pre><code>app.get('/previous', function (req, res){
      var prevMatches = worldcup.getPrevMatches();
  
      console.log(prevMatches);
  
      res.render('prev.html', {
          prevMatches: prevMatches
      });
  });
  </code></pre>
  
  <p>prevMatches is always undefined at this point. I thought that the request package would wait until, well, the request was finished and then proceed with my code. Is this not the case?</p>
  
  <p>Thanks.</p>