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 trialjlampstack
23,932 PointsAJAX Request Question
The illustration above shows in the Web API holding
- setTimeout 1000
-- pushed to Callback Queue first
- setTimeout 3000
-- pushed to Callback Queue third
- AJAX Request
-- pushed to Callback Queue second
Is this because it gets pushed by order of time it takes to complete? Lets assume the AJAX Request was a really large object that takes 10 seconds to download which in turn is longer than the 3 sections it takes for setTimeouts 3000. In this scenario would the AJAX request be pushed to the Callback Queue last?
Also please clarify if the the Callback Queue is the stage the data is downloaded?
Thanks in Advance (=
1 Answer
Zimri Leijen
11,835 PointsThe order of the callback queue is when the callback is triggered, which depends on the callback itself.
In many cases, callbacks happen when the data is received (so, when data was finished downloading, or when you successfully completed reading or writing a file to disk or database, or when another tasks that may take long is completed)
And yes, if the AJAX request would take longer than 3 seconds it would be at the end of the queue.
One thing to remember about async is that the order of execution is not guaranteed, because you don't know in advance how long each task will take (you can have an estimation, but unexpected delays can happen).
jlampstack
23,932 Pointsjlampstack
23,932 PointsThanks Zimri,
I appreciate your response. Just to confirm and for my understanding, does the downloading of data take place while in the Web API stage?
Please let me know so I can mark as best answer to close this question.
Thanks again (=
Zimri Leijen
11,835 PointsZimri Leijen
11,835 PointsYes,
one way to view it, is the Web API stage is like the kitchen, and a callback is like a device you get when you order something at some fastfood restaurant. Your order will be processed in the kitchen (the Web API). And the callback (the device) will 'beep' when the order is done. Then it will be added to the callback queue (you can get your item from the desk).
Joe Elliot
5,330 PointsJoe Elliot
5,330 PointsI just wanna praise how helpful that fast-food analogy was!