Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alternative to async: false ajax

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 ?

like image 568
rpsep2 Avatar asked Feb 28 '14 09:02

rpsep2


People also ask

Is Ajax async false deprecated?

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() .

What is async false in Ajax?

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.

What is difference between async true and false in Ajax?

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 do I fix 405 Method not allowed in Ajax?

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.


1 Answers

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
});
like image 189
jfriend00 Avatar answered Oct 04 '22 01:10

jfriend00