The problem is with variables why the async
function is executing after variable assignment. what can I do for the execute function and wait for it to finish to get the value of the variable?
var request = require('request');
function information(callback) {
var options = {
url: "http://someurl.com",
method: "GET",
headers: {
Token: "MV"
}
}
request(options, function (error, response, body) {
if (!error) {
try {
json = JSON.parse(body);
callback(json);
} catch (error) {
error = 'error'
callback(error);
}
}
})
}
var foo;
information(function (bar) {
if (bar) {
console.log(foo);
foo = bar;
console.log(foo);
}
})
console.log(foo);