Search code examples
javascriptjqueryajaxpromisedeferred

How to handle fail of $.when promise?


I'm not understanding how $.when works during the fail when passing multiple AJAX calls. If one fails, does the fail callback trigger? Also, what is the parameter of the fail, one per ajax or is it one fail shared for all?

$.when(
    $.ajax('/url1'),
    $.ajax('/url2'))
   .done(function (resp1, resp2) {

    }).fail(function (??) {

    });

Solution

  • This should be easy enough to check right?

    var d1 = $.Deferred(), d2 = $.Deferred();
    
    $.when(d1, d2)
       .fail(function (a, b) { 
          console.log(a, b); 
       });
    
    d2.reject("hi!");
    

    The output is hi! undefined. So only one argument gets passed.

    http://jsfiddle.net/22b3L/