How to ensure AJAX requests called in a certain order get the response in the same order?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First, keep in mind that the server itself might not return responses in the order the requests are received. Imagine if you fire a complicated ajax request first, and a simple on second (perhaps referencing some cached static data). The second request will probably return before the complicated one returns. You can see how this complicates things; if the order the requests are received is not the same as the order the responses come back, how can one make assurances?
You have some options:
1) You can make the requests synchronous instead of asynchronous. However, this will lock up the browser.
2) A better approach would be to chain the requests such that the next request only fires after the first is completed, by only firing the second request after the first returns.
As a note, if you find that you have 2 ajax operations that you need to chain, it might make sense to create a server endpoint to handle the 2 operations in one request. i.e., just make an ajax request to do everything you need.