JQuery Postponed. then () is not called after .fail ()

I use jQuery.Deferred and register done , fail and then handlers:

 $.when( some_ajax(url) ) .done(function(result){}) .fail(function(){}) .then(function(){}); //just like that, with a single parameter 

I found that when my ajax call succeeds, the done and then callbacks are called in that order. However, when ajax fails, the fail callback is called, but I do not get the then callback.

I read the jQuery.Deferred documentation but did not find any clue as to the reason for this behavior.

When using always instead of then it is called in both cases - success and failure (first done / fail is called, then always is called). The documentation does not indicate the expected difference between always and then in my described scenario, why do they behave differently?

+8
jquery jquery-deferred
source share
1 answer

The .then() syntax is then (successCallback, failureCallbacl) , so if successful, both are called and only fail if called.

In your case, you only pass one callback to the .then() method, it will be registered as a success callback, so you have two successful callbacks registered in done() , and the other with .then() . But for the error case, you only have one callback registered with .fail()

If you want a callback to be called regardless of success / failure, use . always ()

+19
source share

All Articles