Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Phil Corio
Phil Corio
17,888 Points

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