-1

So I am running a few $http.get requests within a forloop and storing the results within an array. When I run: console.log(episodesObj[0]) it returns undefined. However when I run console.log(episodesObj) it returns the whole array:

enter image description here

Below is the code I am running

    var episodesObj = [];
    for(i=1;i<=num_seasons;i++) {
      $http.get('/api/show/<%=show.id%>/season/'+i)
      .then(response => {
        episodesObj.push(response.data)
      });
    }
    console.log(episodesObj); ```
  • Can you paste your array as code instead of screenshot? – mickl Apr 29 '20 at 21:52
  • 1
    My best guess is lazy console and a dupe of "how to return the result of an asynchronous call", but as you show zero code, it's kinda impossible to answer – ASDFGerte Apr 29 '20 at 21:53
  • @mickl https://pastebin.com/1pN28j82 - Is this what you looking for? – Christopher Pantelli Apr 29 '20 at 21:54
  • Show us the code that is leading up to the console.log (and including) – Sturm Apr 29 '20 at 21:54
  • @Sturm I have edited the question to include the code. – Christopher Pantelli Apr 29 '20 at 21:56
  • The callback provided to `$http.get().then` is not guaranteed to have finished before your console.log statement. – Sturm Apr 29 '20 at 21:56
  • Well, i guess i was right. Glass-ball wins this time. – ASDFGerte Apr 29 '20 at 21:56
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Dexygen Apr 29 '20 at 22:02
  • Also related, as mentioned above: [is-chromes-javascript-console-lazy-about-evaluating-arrays](https://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) - you can clearly see in your screenshot, that the initial array it displays is empty. You just clicked and expanded it after the results were already in, and the lazy evaluation then displayed those. – ASDFGerte Apr 29 '20 at 22:06

2 Answers2

0

If you want to print the final result, you could use promise all

all(promises);

Combines multiple promises into a single promise that is resolved when all of > the input promises are resolved.

Something like:

var listOfPromises = [];
for(i=1;i<=num_seasons;i++) {      
    listOfPromises.push($http.get('/api/show/<%=show.id%>/season/'+i))     
}


$q.all(listOfPromises).then(function(result){
   
    // we run this code once all promises are resolved
    for (var i = 0; i < result.length; i++){
        episodesObj.push(result[i]);
    }
   
   console.log(episodesObj);
});
Community
  • 1
  • 1
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
0

You're not pushing the response to your array until after the http call completes, so if you immediately log episodesObj[0], of course it's undefined.

Chrome keeps live references to non-primitive things you console log, so when the http calls eventually update the array, it's also updated in the console, which is why you're able to view it later.

frodo2975
  • 10,340
  • 3
  • 34
  • 41