Twitch.tv projct help!

Hello, I’m currently working on my Twitch Tv project .I can get the data about each channel perfectly fine, but it doesn’t seem to respect the order of the items in the array when it’s displayed in the console. And I seem to get all of the stream states displayed for each channel whenever I run the code!

The order of the names is irrelevant. If the data from multiple calls needs to be sorted, you’ll have to do it in the client code. You will, however, run into an issue with the use of i in your async callback. Check out this article for more details: http://www.jstips.co/en/javascript/implementing-asynchronous-loops/

1 Like

I’m not clever enough to give you a hint, without giving you a straight solution. But here’s what I did to make things load in order:

Step 1: Make a function whose purpose it is to load ONE Twitch user. You will pass in an index to this function which will check a presumably global array of user names.

Step 2: This function will make an ajax call to get the user data. I then made another ajax call within the ‘done()’ function to get the stream status of it (the ajax-within-an-ajax method allows you to be sure all of the first call is complete before trying to make the second call)

Step 3: Within this second ajax request (remember, it is inside the done function of the first one) you should have all the data you need to push this exact user onto a data array. In my case, I just called a function whose purpose it was to format that data in a useful way.

Step 4: Check to make sure the index passed in isn’t the last index in the array - if it’s not the last index, then now recursively call this same function again, only adding 1 to the index originally passed in. By containing this in an ‘if’ statement, it will gracefully stop calling itself once all of the users have been parsed.

This will also ensure that all users are appended to the DOM in sequential order.