could someone help me with this code? It is simplified to see the problem easier, which is the variable myGlobalVar
does not behave like global.
var request = require('request');
var myGlobalVar = "myglobalstring";
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);
myGlobalVar = info.stargazers_count + " Stars";
console.log(myGlobalVar + "-1");
}
//myGlobalVar = info.stargazers_count + " Stars";
// console.log(myGlobalVar+"-2");
}
request(options, callback);
console.log(myGlobalVar + "-3")
The result of this is
myglobalstring-3
23038 Stars-1
23038 Stars-2 (Uncommenting those lines)
The variable does not keep the value outside the function...