title: Node.js request function return data
body: <p>I have gone through questions about returning data from a node JS request call. A common mistake is assuming statements are executed line by line or synchronously, which is not the case here. A question of this type: <a href="https://stackoverflow.com/questions/19539391/how-to-get-data-out-of-a-node-js-http-get-request">How to get data out of a Node.js http get request</a>. </p>
  
  <p>My question is a bit different. I have written a function getNumber() which returns the number of results for given query. I am wondering how I return a value retrieved by the callback function? So for instance: </p>
  
  <pre><code>function getNumResults() { 
  
  Request(url, function(response) { 
  
      var responseJSON = JSON.parse(body); 
      return(responseJSON.results.count); 
  
     }); 
  </code></pre>
  
  <p>} </p>
  
  <pre><code>function Request(URL, callback) {
  
  request(URL,  function(error, response, body) { 
  
      console.log('URL: ', URL); 
      console.log('error: ', error); 
      console.log('statusCode: ', response &amp;&amp; response.statusCode); 
      console.log('body: ', body); 
      callback(body); 
  }); 
  </code></pre>
  
  <p>}</p>
  
  <p>What if I want getNumResults() to return responseJSON.results.count? How could I do this? </p>