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 AJAX Basics (retiring) AJAX and APIs Displaying the Photos

Przemyslaw Mazur
PLUS
Przemyslaw Mazur
Courses Plus Student 9,296 Points

Why this code works ?

My question is why the displayPhotos function works before $.getJSON runs?

1 Answer

I Dilate
I Dilate
3,983 Points

$.getJSON sends a request to a server, which is likely to be slower returning than the time that the rest of the code in your script takes to be executed.

Remember a few lessons back, you were told that AJAX queries return at different times, and you can't predict which one comes back first? This is a good example.

Note: your question was a little vague - this is the best I can do to answer it without you providing more information. If you're experiencing a specific problem with this challenge, can you elaborate please?

I Dilate
I Dilate
3,983 Points

Note, that the A in AJAX stands for "Asynchronous" - and this is exactly what it means.