I loop through an array running an ajax request for each. I need the requests to happen in order, so i can pick up the last request and run a function on success.
at the moment im running (simplified):
$.each(array, function(i, item){
ajax_request(item.id, item.title, i);
})
function ajax_request(id, title, i){
$.ajax({
async: false,
url: 'url here',
success: function(){
if(i == array.length-1){
// run function here as its the last item in array
}
}
})
}
however, using async:false makes the application unresponsive/ slow. but, without async:false sometimes one of the requests will hang a bit and actually return after the last sent ajax request returns.
how can i implement this without using async:false ?
As of jQuery 1.8, the use of async: false with jqXHR ( $.Deferred ) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() .
Setting async to false means that the statement you are calling has to complete before the next statement in your function can be called. If you set async: true then that statement will begin it's execution and the next statement will be called regardless of whether the async statement has completed yet.
by default async is true. it means process will be continuing in jQuery ajax without wait of request. Async false means it will not go to next step untill the response will come. By default async is true process will be continuing in jQuery ajax without wait of request.
How to solve it? The simplest way is to enable CORS (enable the necessary headers) on the server. If you don't have server-side access to it, you can mirror the web service from somewhere else, and then enable CORS there.
You can use a local function for running the ajax call and in each successive success handler, you can kick off the next ajax call.
function runAllAjax(array) {
// initialize index counter
var i = 0;
function next() {
var id = array[i].id;
var title = array[i].title;
$.ajax({
async: true,
url: 'url here',
success: function(){
++i;
if(i >= array.length) {
// run function here as its the last item in array
} else {
// do the next ajax call
next();
}
}
});
}
// start the first one
next();
}
Updating this answer in 2016 with an option that uses promises. Here's how you run the requests in series:
array.reduce(function(p, item) {
return p.then(function() {
// you can access item.id and item.title here
return $.ajax({url: 'url here', ...}).then(function(result) {
// process individual ajax result here
});
});
}, Promise.resolve()).then(function() {
// all requests are done here
});
Here's how you run them in parallel, returning all the results:
var promises = [];
array.forEach(function(item) {
// you can access item.id and item.title here
promises.push($.ajax({url: 'url here', ...});
});
Promise.all(promises).then(function(results) {
// all ajax calls done here, results is an array of responses
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With