javascript - node.js - making result available globally (Request module) -


i'm trying process returned json result request need expand scope outside request call. declared data variable empty string , assign result data doesn't print result.

how can accomplish this?

module.exports = function(callback) {      var request = require("request")     var url = "http://sheetsu.com/apis/94dc0db4"     var data = "";      request({         url: url,         json: true     }, function (error, response, body) {          if (!error && response.statuscode === 200) {             callback(body)              data = body;          }     })      console.log(data);   } 

your script executed in order:

  1. request() executed
  2. console.log(data)
  3. request() callback function, asign data value

if want print data, must inside request callback function. async module, useful when performing async tasks, specially if need perform tasks in specific order , use data requests.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -