I'm pinging the twitch API with a list of users, and trying to add the Json from each to an array. The for loop iterates through the list of users and returns the json for each call with each individual name. If I put the console.log(arr) inside the for loop, I get 8 logs each with an additional users Json tacked on, which I would expect. However, when I call console.log(arr) outside of the for loop, it returns an empty array. I setup the variable before going into the for loop, so it should be a global variable, no? Why is the information lost after the for loop?
var users = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"]
var headers = {
'Client-Id': 'xxxxxxxxxxxxxxxxxxxxxxxxx'
}
var arr = [];
for (i = 0; i < users.length; i++) {
var url = "https://api.twitch.tv/helix/users?login=" + users[i];
fetch(url, {headers})
.then(function(response) {
return response.json();
})
.then(function(myJson) {
arr[i] = myJson;
});
}
console.log(arr);