Search code examples
jqueryajax.when

Can I rely on the order of responses from jQuery.when on multiple Ajax requests


Per this post: https://stackoverflow.com/a/17548609/985704

Using jQuery.when to perform multiple simultaneous ajax requests.

var requests = Array();
requests.push($.get('responsePage.php?data=foo'));
requests.push($.get('responsePage.php?data=bar'));

var defer = $.when.apply($, requests);
defer.done(function(){

    // This is executed only after every ajax request has been completed

    $.each(arguments, function(index, responseData){
        // "responseData" will contain an array of response information for each specific request
    });

});

When all requests are done, can I be sure that the arguments (of the $.each) are in the same order as the requests? Is this documented somewhere?


Solution

  • Per JasonP: (thank you)

    Yes. "The arguments passed to the doneCallbacks provide the resolved values for each of the Deferreds, and matches the order the Deferreds were passed to jQuery.when()." api.jquery.com/jQuery.when