Reputation: 99
Hey guys I'm having trouble using the "appIds" variable outside the function scope, how can this be done? Here's the script below used in NodeJS:
var appIds = [];
request("http://api.steampowered.com/ISteamApps/GetAppList/v2?format=json", function(error, response, body) {
if (!error && response.statusCode == 200) {
var o = JSON.parse(body);
appIds = o.applist.apps.map(v => v.appid);
// console.log(appIds); works within scope.
}
});
console.log(appIds);
Upvotes: 0
Views: 46
Reputation: 121998
That is wont work since it is an asynchronous call. You can do only inside the callback.
What you are doing in the commented code is the right way to handle it.
request("http://api.steampowered.com/ISteamApps/GetAppList/v2?format=json", function(error, response, body) {
if (!error && response.statusCode == 200) {
var o = JSON.parse(body);
appIds = o.applist.apps.map(v => v.appid);
console.log(appIds); //works within scope.
}
});
Upvotes: 3