This is for my Twitch TV App.
I want to push data from get requests into an array.
On line 43 of my Javascript, I print the length of the array into the console. It should always equal 6, but when I run it multiple times I get 4, 5, or 6. I want to understand why it does this and how to get a solution.
I think it might have to do with the asynchronous behavior of get requests, but I don’t understand.
Thanks for any help!
edit: I found a solution by putting console.log(arr.length)
into a setTimeout function, like this: setTimeout(function(){console.log(arr)},1500);
. I also had to put it outside of my forEach method so it would only run the code once.
I don’t know if this is bad code, but the push method is putting all the code I want into the array. Now I can append the DOM just once after I join everything in the array.
I’m certain this is due to the asynchronous behavior of get requests. I hope this helps someone in the future.