[HELP] Twitch API

[HELP] Twitch API
0

#1

Hi,

my .getJSON is not working when I try to use console.log(user) can please someone help me try to figure that out? Maybe I did something wrong in my for loop or url’s I just don’t know yet. I’m trying to make the script give me the information about streamers that are not available anymore, that’s why I used ‘‘data3.error’’ but it seems to not work.


#2

The code inside the following for loop never executes, because following is an empty array at the time the for loop is encountered in the code execution.

  //Pegando informação do array de streamers//
  for (var i = 0; i < following.length; i++) {

Why? Because getJSON is asynchronous, it does not prevent other synchronous code after it from executing while it awaits a response. When the response comes back for the call to followerURL, then following does get data pushed into. The problem is, the for loop (shown above) is evaluated almost immediately after the call to getJSON is made and since it takes longer for the data to come back from the call to followerURL, then following.length is 0, so the code inside the above for loop never runs.

If you want any code to work with the following array, then it will need to done after you have pushed all the values into it inside first getJSON callback function.

Keep in mind, you will need to think about the same asynchronous nature of getJSON inside this for loop, to make sure you get the data you want.


#3

You said a lot of things that I don’t know about. I will try to learn them and see what I can do about my code.

Thank you very much.


#4

Sorry to jump into this coversation.I came across the same problem once.@RandellDawson - how can we ask computer to execute ramaining set of codes after we received JSON data from the asynchronous call? Or Is there a workaround for this?


#5

@Newt - You can get an understanding about synchronous and asynchronous calls from below article.


#6

In the case of getJSON, the response data is available via the argument of the callback function. If you want to work with this data (i.e. manipulate, display, etc…) it must be within the callback function. You can always call other functions from within the callback function.

  var url = "someAPIUrlGoesHere";
  $.getJSON(url,function(data){
    // code to work with data goes here
  });

#7

That Clarified my issue. :slight_smile:
Thanks a lot!
:+1: