Twitch w/Angular

Need help with this. My for loop only captures the last element in the array.

Hi there!

This happens because you’re calling a asychronous function inside a for loop. When the result returns, your loop already have finished, and only the last value of the index is considered (remember the scope of a var variable in JS).

To solve that there’s more than one solution. I suggest, first, learn more about this problem. Some useful links:

I see in your code that you are using a IIFE function, and this is a possible solution. You can also pass the value and the index in the streamStatus function parameter (I really don’t know if this is the best solution).

Good study!

Thanks for the help! The links were very helpful.

I’ve modified the app to just focus on the loop. I am still not getting the full loop. What else am I missing?

EDIT: I was wrong, the loop is working correctly. The problem was my limited understanding of the ng-repeat for Angular. Through testing I found the array was being populated correctly. Thanks again!

1 Like