9

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?

Haji
  • 1,715
  • 7
  • 25
  • 41
  • Your call to `then()` only handles success, so it is quite normal for it not to handle the fail case (I think that's what Arun P Johny says in his answer). Or do you mean you're also passing a fail handler to `then()` in your actual code? – Frédéric Hamidi Apr 21 '13 at 08:16
  • It is indeed clearer, and Arun's answer is right: your call to `then()` only handles success, not failure, so it is logical for the handler not to be called in case of failure. – Frédéric Hamidi Apr 21 '13 at 08:37
  • now a close vote for too localized !!!! – Arun P Johny Apr 21 '13 at 08:52

1 Answers1

21

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()

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • 1
    I suspect your answer is right (we need more information from the questioner to be sure), but maybe it is not clear enough. I would suggest you try to clarify what you mean by *in case of success both are called*, right now it can be understood as *both the success and the failure handlers are called*, which is wrong. (In passing, I tried to offset the downvotes, but you probably should delete your immediately previous comment: it is redundant, can be seen as offensive, and does not help your case). – Frédéric Hamidi Apr 21 '13 at 08:20
  • @FrédéricHamidi yes probably, but I had enough of some crazy downvotes – Arun P Johny Apr 21 '13 at 08:26
  • @FrédéricHamidi sometimes even think of leaving the forum altogether – Arun P Johny Apr 21 '13 at 08:26
  • Sometime back I found another answer http://stackoverflow.com/questions/16115180/add-two-li-elements-into-the-end-of-the-ul/16115214?noredirect=1#comment23028862_16115214 which was downvoted, I also provided same answer and it was accepted. But the another user was downvoted – Arun P Johny Apr 21 '13 at 08:31