0

New JavaScript user here. Still learning the ways of Asynch and callbacks.

The, console.log(current_email) within the callback works (prints the expected email).

request.get(options, function(error, response, body) {
    current_email = body.email;
    console.log(current_email);
});

However, the last console.log(current_email) prints out nothing (the initialized value). I assume this is something related to Asynch. I have looked at similar problems/solutions, but was unable to apply them to my code after 3 hours. Does anyone have a solution to my issue? Please.

var current_email = "";    
var options = {
    url: 'https://api.spotify.com/v1/me',
    headers: { 'Authorization': 'Bearer ' + access_token },
    json: true
};

request.get(options, function(error, response, body) {
    current_email = body.email;
    console.log(current_email);
});
console.log(current_email);
larson-adam
  • 57
  • 3
  • 8

0 Answers0