The Problem
When working with AJAX to query a remote API, by the asynchronous nature of the request it comes back whenever it completes. The problem is when I have to make iterative calls to the same API with different criteria, I don't know which response is coming back.
The question: Is it possible to pass a variable from
Sample code: (simplified)
n=5;
for(i=0; i < n; i++) {
$.ajax({
url: someURL,
method: post,
// I don't want to have to use async: false, that's bad
// async: false,
data: JSON.stringify(someData),
beforeSend: function(){
console.log("Starting request #"+i)
},
error: function(err, code, text) {
alert("Something went wrong: \n\n" + code + ": " + text);
},
success: function(response) {
console.log("Response for request #"+i)
console.log(response)
}
})
}
The problem comes up in that final success function: What I should see is:
Starting request #0
Starting request #1
Starting request #2
Starting request #3
Starting request #4
Response for request #2
[object]
Response for request #1
[object]
Response for request #4
[object]
Response for request #0
[object]
Response for request #3
[object]
What I actually see is:
Starting request #0
Starting request #1
Starting request #2
Starting request #3
Starting request #4
Response for request #4
[object]
Response for request #4
[object]
Response for request #4
[object]
Response for request #4
[object]
Response for request #4
[object]
This is important not because I care about my logs being right, but because the actual version needs to be able to reconcile the responses with what was sent. I don't want to go synchronous on this, because it'll be slow, annoying, and possibly time out.