title: How to return values from functions making HTTP request in node.js?
body: <p>The getStockValue() function is called from another javascript file in the following way:</p>
  
  <pre><code>var r=require("./stockfile");
  var returedData = r.getStockValue());
  </code></pre>
  
  <p>here returnedData contains only "-START-".</p>
  
  <p>My objective is to get the body object returned from the function, <strong><em>after</em></strong> receiving the response. I've tried putting a return statement into the '<strong>close</strong>' event handler, but it didn't work.</p>
  
  <p>How should I do that?</p>
  
  <pre><code>function getStockValue() {
  
      var http = require('http');
  
      var options = {
          host: 'in.reuters.com',
          path: '/finance/stocks/overview?symbol=RIBA.BO',
          method: 'GET'
  
      };
  
      var body = "--START--";
  
      var req = http.request(options, function (res) {
          console.log('STATUS: ' + res.statusCode);
          console.log('HEADERS: ' + JSON.stringify(res.headers));
  
          res.on('data', function (chunk) {
              body += chunk;
  
          });
  
          res.on('close', function () {
              console.log("\n\nClose received!");
          });
  
      });
  
      req.on('error', function (e) {
          console.log('problem with request: ' + e.message);
      });
      req.end();
  
      return body + '... recieved';
  }
  
  exports.getStockValue = getStockValue;
  </code></pre>