$.ajax is asynchronous. Your second call to helix/streams fires before the aoathToken one has finished. $.ajax also doesn’t return its results, but passes them to the callback (your success function, which you correctly do have there).
Thanks for your response. Just for the sake of testing, I set the aoathToken request to async: false so I am sure that my token is back before triggering the sencond request. But I am still getting the same response
The calls are asynchronous so that they do not block UI updates and the page stays responsive to the user.
What you should do instead of disabling async is use the callback function to fire the additional request, along the lines of:
// -snip-
success: function(token) {
console.log(token);
// token will be something along the lines of
// "{\"access_token\":\"sometoken\",\"refresh_token\":\"\",\"expires_in":3600,\"scope\":[]}"
// parse the json string in token, see JSON.parse()
// call getStream with the parsedData.access_token
}
})
function getStream(token) {
$.ajax({
type: "GET",
url: "https://api.twitch.tv/helix/streams?user_id=freecodecamp",
// -snip-
})
}
This way your second ajax will be called after the first one has successfully returned without negative user experience from blocking synchronous requests.