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:
request()
executedconsole.log(data)
request()
callback function, asigndata
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
Post a Comment