[Solved] Twitch API Project - Issue waiting for data to return from Ajax call

I’m working on the Twitch streaming app, and I’m having an issue pulling the data from Twitch. I’m making my Ajax call in a for loop (so I can iterate through several different streamers), but the loop isn’t waiting for the data to return before moving on to the next iteration.

Here’s an image of my issue:

In the above, I’m console logging test prints of the for loop iterator. The loop is pushing through and not getting data back until the seventh call.

My code is here: https://github.com/joshamore/twitch_stream_checker/blob/master/script.js

Any idea how I can make this work?

Thanks all!

In your for loop replace var with let

1 Like

That worked. Thanks!

I’ve looked up let, but I’m not sure why that fixed the issue – isn’t let just a variable with a restricted scope?

Found this YouTube video. It explained the issue. Thanks again for the help!

I recommend you to read YDKJS (let vs var vs const is addressed in the second book’s 3rd chapter).

1 Like