Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AngularJS Services and Dependencies Connecting to an External API

Stephen Poole
Stephen Poole
9,361 Points

Why can't we simply "replace the variable entirely"?

In the video, he uses a for loop to put the JSON objects into the people array, and says this is necessary because "you cannot simply replace/reassign the variable entirely".

I don't at all understand why this is the case. If someone could provide a clear explanation as to why this is necessary it would be greatly appreciated! Thanks! :)

1 Answer

I believe this is due to the nature of the javascript request being asynchronous. This article may help.

http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call