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 Asynchronous Programming with JavaScript What is Asynchronous Programming? The Callback Queue and Event Loop

Begana Choi
PLUS
Begana Choi
Courses Plus Student 13,126 Points

the sequence of API that pushes task to the callback queue

the examples are quite understandable which API pushes to the callback queue. (because there are some task by setTimeout) on the other hand, if there are only several AJAX requests on the API, can't we know which AJAX request pushes to the callback queue first?

2 Answers

They get called in the order of the program flow, normally from top to bottom.

However, you have no control over when they will be finished, as that will depend on which AJAX request takes longer to finish. Which may depend on network as well. The order in which they were called isn't very important, as asynchronous calls will run independently of each other and they don't need to wait for other tasks to complete.

It usually doesn't matter in which order they finish, but if you need data from one AJAX request before making another request you'd need to write your code in a specific way to make sure you have that data.