Results I hope to log: [ 'freecodecamp' , 'twitchtv' , 'exercise' ]
I've read a bunch of post regarding Asynchronous situation which explains why I'm getting these "empty [ ]" on the Console:
"empty" meaning it appears like this:[ ],
rathan than this : [ 'freecodecamp' , 'twitchtv' , 'exercise' ].
What I'm getting so far : if console.log(online_streamer) is placed as follow scenarios:
A. (Within getJSON callback) --- logs: undefined, then a couple lines of empty [ ] before printing the final "usable" array with 3 items(gamers names) in it.
B. (Outside forLoop) --- logs: 1 [ ] array with 3-items to show ONLY if I press the arrow down button. Again, (Async situation.)
My Question : Did I misplace the consoe.log or do I need some complete different function to print a usable array like: [ 'item1' , 'item2' , 'item3' ] like the last iteration of scenario A.) ? Thanks guys.
function getOnlineGamers(){
var online_gamers = [];
for (var i=0; i<gamers.length; i++){
//(gamers is a defined array with 11names of 11gamers.)
$.getJSON(url+"streams/"+ gamers[i]+cid).done(function(data){
if(data.stream === null){
}else {
var name= data.stream.channel.name;
online_gamers.push(name);
}
console.log(online_gamers); // <--- A
});
};
console.log(online_gamers); //<--- B
};
console.log(getOnlineGamers())