I’ve just finished the TwitchTV challenge. I found it very difficult. I have been able of finishing with help from internet.
The main difficulty has been to make work a $.getJSON method nested within another one.
As the call to the JSON object with .getJSON method is asyncronous the nested call doesn’t return anything.
I found the way with help, putting all the code within a function and setting a counter equal to the lenght of the users array, decrementing in one for each of the responses to the nested JSON calls; so only when all the responses are received is when the function makes something with the html in response to the call of the function.
I would still need some more enlightenment about JSON asyncronous and I would like to know if there are other ways more simple (e.g $.ajax method with the async property, but the documentation says ‘Cross-domain requests and dataType: “jsonp” requests do not support synchronous operations’).
Hey @cortazar11 I think people are doing the JSONP the way they are is to make the website more responsive. It can start to populate the page as soon as the first Async response is received back without waiting for everything to be returned.
The reason the second JSONP call is nested inside the first is to fetch the Twitch TV channel image only after the main channel information is returned. Notice how the second $.getJSON() is nested in the first calls .done section. This insures things happen in the correct order.
It took me a long while to make it all work, and I guess that was part of the point of the challenge.
Hey @jpdrecourt I agree with you, but I think a lot of us are using ES5. With ES6 we would have to run something along the lines of Babel. I haven’t got that far along in my studies. But perhaps I should start using ES6 sooner then later…
I am starting to learn ES6 right now using Babel and with a bit of Lebab help. I have been having a lot of trouble with the Twitch Viewer as well. All the info I am learning here makes me realize that perhaps I should not be using $.getJSON, because I was initially only able to access the users that were online. Then I got offline users, but any account that was deactivated or didn’t exist came back undefined. The thing is that all the native text editor algorithms etc only take ES5, which makes starting to use ES6 for this one project a bit strange. However, since I do want to progress with my ES6 education, maybe I will give it a shot. After all, I have to get started on ES6 somehow!
Hey @interglobalmedia Maria, there are some editors that have built in support for ES6, but except for the one I use I’m not sure what the other ones may be, perhaps someone can add to the list.
I’m using WebStorm and it does ES6 code highlighting, code completion etc. right out of the box. I’m sure that there are others.
I was going to get WebStorm a little while ago, and then got sidetracked. I use PHPStorm for WordPress Development, so I love JetBrains products. I guess I will just have to make sure I get it up and running this weekend! Thanks for letting me know about it!
BTW, you can use ES6 in Sublime Text 3 as well. I do, and have had no problems with it so far.
For this I used angular, they have a great FREE tutorial on their main website.
First I initialised each stream in an object as offline, then used a for loop on an array of channel names to $http.GET each stream from the api. If they were online I updated each stream object with the relevant details.
If I did it again I might try a render function at the end of the callback that goes through my list of streams and adds them to my page if they’re not there already. That should dynamically update the page when data is received I think.
I’m kinda stuck on this project too. It looks like the last console.log is returning an empty array and executing before the loop while the console.log nested in the for loop and getJSON is returning values. Reading How do I return the response from an asynchronous call? but not sure how to do the callback or promises with my code
function channel() {
var channels = [“ESL_SC2”, “OgamingSC2”, “cretetion”, “freecodecamp”, “storbeck”, “habathcx”, “RobotCaleb”, “noobs2ninjas”];
var channelData = ;
I don’t know if you still need help on this, but it may be useful for others.
I thinkl you need to wrap your console.log into an anonymous function for it to work: