0

I need to wait multiple deferred to complete and then process my success or failed callback.

function myFunc(){
    alert("Success");
    }
function myFailure(){
    alert("Failure");
    }

var jqxhr = $.ajax( "example.cfm" )
    .done(function() { alert("Primo report"); })
    .fail(function() { alert("Primo report non completato"); })
    .always(function() { alert("Finito prima chiamata"); });

var jqxhr2 = $.ajax( "example2.cfm" )
    .done(function() { alert("Secondo report"); })
    .fail(function() { alert("Secondo report non completato"); })
    .always(function() { alert("Finito seconda chiamata"); });

var jqxhr3 = $.ajax( "example3.cfm" )
    .done(function() { alert("Terzo report"); })
    .fail(function() { alert("Terzo report non completato"); })
    .always(function() { alert("Finito terza chiamata"); });


    $.when(jqxhr,jqxhr2,jqxhr3)
    .then(myFunc, myFailure);

The problem is that if one of my ajax request fail, immediately is fired the callback.

It's possible to wait until all the ajax request are complete and then callback?

Tropicalista
  • 3,097
  • 12
  • 44
  • 72
  • It is possible to wait for all three to complete, however you'll have to do it with a counter, $.when will always return when one of the passed in promise objects are rejected. – Kevin B Feb 06 '13 at 15:58
  • No one could help with the solution? I've read some similar question, but cannot understand how proceed with my code... – Tropicalista Feb 06 '13 at 16:02
  • If any of the `Deferred` objects are rejected, `$.when` will immediately invoke the `fail` callbacks. That's the way it's designed to work. – jbabey Feb 06 '13 at 16:06
  • I have seen here: http://stackoverflow.com/questions/6538470/jquery-deferred-waiting-for-multiple-ajax-requests-to-finish that this is possible, but cannot understand how, because I'm new to jquery and deferred.... – Tropicalista Feb 06 '13 at 16:09
  • Duplicate of what? I've search for two hour an answer on the site, have found nothing... – Tropicalista Feb 08 '13 at 01:29

0 Answers0