最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multiple Ajax Requests (with one callback) - Stack Overflow

programmeradmin2浏览0评论

I'm sending multiple ajax requests and want to get a callback if all the requests are successful. I've found $.when($.ajax(), [...]).then(function(results){},[...]); but that only works when you know in advance how many you're going to do. In my case, it varies depending on user input.

I tried the following but I'm not sure where or how $.when fits in:

$.when(
    $('#piecesTable tr').not(':first').each(function(){

        // ...some prep...

        $.ajax({
            // ...args here...
        });
    })
).then(function() {
    // All requests are done
});

How do I use the results of all those separate $.ajax calls with $.when? Or do I handle this some other way?

I'm sending multiple ajax requests and want to get a callback if all the requests are successful. I've found $.when($.ajax(), [...]).then(function(results){},[...]); but that only works when you know in advance how many you're going to do. In my case, it varies depending on user input.

I tried the following but I'm not sure where or how $.when fits in:

$.when(
    $('#piecesTable tr').not(':first').each(function(){

        // ...some prep...

        $.ajax({
            // ...args here...
        });
    })
).then(function() {
    // All requests are done
});

How do I use the results of all those separate $.ajax calls with $.when? Or do I handle this some other way?

Share Improve this question edited Aug 5, 2016 at 14:44 T.J. Crowder 1.1m200 gold badges2k silver badges2k bronze badges asked Aug 5, 2016 at 13:59 H.CH.C 6081 gold badge10 silver badges30 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

I think the general structure of what you're looking for is something like this:

var requests = [];

// Populate requests array with ajax requests.
requests.push($.ajax({
    // ...
}));

// Add as many requests as you want to the array.

$.when.apply($, requests).done(function() {
    var args = $.prototype.slice.call(arguments);

    // args should contain the results of your ajax requests.

    // Do whatever with the results.
});

There's a modern alternative to the $.when.apply($, arrayOfPromises) beast: Promise.all:

Promise.all(arrayOfPromises).then(function(arrayOfResults) {
    // Use arrayOfResults
});

Promise.all expects an array of thenables and returns a promise that resolves with an array of the results when all of the thenables have resolved. It's fine with jQuery's promises, because all it requires is that they be thenables, which they are.

You can use this on any browser with Promise support, or if you include a Promise shim/polyfill.

So in your case, you'd build the array:

var arrayOfPromises = [];
$('#piecesTable tr').not(':first').each(function(){

    // ...some prep...

    arrayOfPromises.push($.ajax({       // ** Push this promise into the array
        // ...args here...
    }));
});

(Or you can build it with $.map or Array#map.)

And then use the array:

Promise.all(arrayOfPromises).then(function(arrayOfResults) {
    // Use arrayOfResults
});

From the jQuery documentation :

$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).then( myFunc, myFailure );

Execute the function myFunc when both ajax requests are successful, or myFailure if either one has an error.

So you can use this system to send variable number of requests and still have the same callback functions :)

Adapted with @TW80000 answer :

var requests = [];
 ....
$.when.apply($, requests ).done( myFunc, myFailure );
发布评论

评论列表(0)

  1. 暂无评论