javascript - Accessing request results in global object -


this question has answer here:

how can make request results available property of object? @ moment, console.log(album.artistname) returns undefined. however, if console.log album.name within request function, console return album name.

i'd able create own object includes data points need can access universally.

var request = require('request');  var album = {};     album.max = 'max';     album.artistname = artistname();  function artistname(){ request('https://api.spotify.com/v1/albums/4inyhv55ddxe0a2tnvkezm', function(err, response){   if(!err && response.statuscode == 200){     var data = json.parse(response.body);     album.name = data.artists[0].name;     }   }); }  console.log(album); 

add this, , restart app, make fast request (you have 10 seconds):

settimeout(function(){     console.log(album); }, 10000); 

but question doesn't make sense. you'll need trigger code, , when you'll not in global scope, though shown here you'll see global variables.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -