Willem Mulder
Willem Mulder

Reputation: 13994

Use jQuery Deferred.done().fail() or Deferred.then()?

I know that Deferred.done(a).fail(b) is identical to Deferred.then(a,b), apart from their syntax, but I want to know if there's a preferred way of syntax.

Why would I use one over the other? Is there a best practice? The only argument that I can come up with right now is that if I encounter the fail function 'b' in the .done(a).fail(b) situation, I know immediately that it is a fail function when I see fail(b), while I do not see that when I just see b in the then(a,b) situation. Any more arguments?

Upvotes: 1

Views: 799

Answers (2)

Ibraheem
Ibraheem

Reputation: 2358

Use Deferred.then(a,b) when you have multiple tasks to get done or fail on.

e.g.

$.when(task1)
.then(task2, task1Failure)
.then(task3, task2Failure)
.fail(task3Failure);

or

$.when(task1)
.then(task2, task1Failure)
.then(task3, task2Failure)
.then(task3SuccessMeaningAllSuccess, task3Failure);

Upvotes: 3

Willem Mulder
Willem Mulder

Reputation: 13994

.done(a).fail(b) is a more semantic approach, so I go for that.

Upvotes: -1

Related Questions