In one of the examples in the request's documents shows this example:
https://www.npmjs.com/package/request#custom-http-headers
var request = require('request');
var options = {
url: 'https://api.github.com/repos/request/request',
headers: {
'User-Agent': 'request'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info.stargazers_count + " Stars");
console.log(info.forks_count + " Forks");
}
}
request(options, callback)
Lets' say I want the value of the variable info return to me.
How do I do it?
You cannot pass a value from an asynchronous command. The most common strategy used in Node.js is to wrap the code that requires the info
variable in a function and call that from the callback.
eg:
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
functionThatUsesInfo(info);
}
}