I'm using jQuery.Deferred and registering the done
, fail
and then
handlers:
$.when( some_ajax(url) )
.done(function(result){})
.fail(function(){})
.then(function(){}); //just like that, with a single parameter
I've found that when my ajax call succeeds, done
and then
callbacks are called, in this order. However when the ajax fails, the fail
callback is called but I don't get to the then
callback.
I've read the jQuery.Deferred documentation but couldn't find a hint regarding 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 doesn't seem to indicate an expected difference between always
and then
in my described scenario, why do they behave differently?
The syntax of .then()
is .then(successCallback, failureCallbacl) that is why in case of success both are called and in case of failure only fail
is called.
In your case you are passing only one callback to the .then()
method, it will be registered as a success callback, so you have two success callbacks one registered with done()
and another one with .then()
. But for error case you have only one callback registered with .fail()
If you want a callback to be called irrespective of success/failure then use .always()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With