30

Update: This question is now out of date as the documentation is accurate and up to date.

I've been exploring the jQuery Deferred/Promise API for a bit and I'm very confused about the differences between pipe() and then() philosophically and in the jQuery documentation. I've found that pipe() is just an alias for then() as of jQuery 1.8.

From jQuery source:

// Keep pipe for back-compat
promise.pipe = promise.then;

Yet the documentation is completely different for pipe() and then() as they supposedly have entirely different uses.

Description for then():

Description: Add handlers to be called when the Deferred object is resolved or rejected.

Description for pipe():

Description: Utility method to filter and/or chain Deferreds.

I understand that historically they had slightly different behavior, but in the entirety of the documentation for pipe or the documentation for then, it doesn't even say that these two functions do the exact same thing now.

So, here's my two part question:

  1. Why does the documentation between pipe() and then() differ as of jQuery 1.8?
  2. Why does then() return a new deferred object? This behavior is completely undocumented (the docs just say it returns a Deferred, not that it's a new one). I understand that it has utility in doing so (namely to implement all of pipe()'s features), but philosophically why is it the case? It's unnecessary given the description of then() (to attach handlers).

Update

I'll even go so far as to say the then() docs are misleading and inaccurate:

Since deferred.then returns the deferred object, other methods of the deferred object can be chained to this one, including additional .then() methods.

Maybe it's just vague, but it implies it returns the deferred object you called then() on for chaining, when in reality it returns an entirely new object....

Update Again

Seems the documentation is simply wrong/out of date! So that answers why the documentation makes no mention of them being the same thing. However, my second question still stands. Is the reason then() returns a new deferred simply so that it and pipe() can be made equivalent?

Adam Terlson
  • 12,610
  • 4
  • 42
  • 63
  • 8
    +1 Why would *anybody* want to close this as being "too localized"? This is a fantastic question!! – Joseph Silber Aug 17 '12 at 19:30
  • I think the biggest issue with the docs for this part of the API is to not just explain that it's a basic CommonJS Promise/A implementation and refer people to somewhere else for an explanation of promises. The word "promise" does not even appear on the "then" page. – Jamie Treworgy Aug 17 '12 at 19:30
  • 1
    @Joseph, right now it is, but when the documentation gets updated a few days/hours from now it might not be anymore. I cannot predict what the new documentation will say, though, but I do think updating the question so it focuses less on the docs and more on the behavior change will prevent others from voting to close it. – Frédéric Hamidi Aug 17 '12 at 19:32
  • @FrédéricHamidi - Well, the main point of the question *is* about the difference between `pipe` and `then`. The doc's inconsistencies is just a byproduct. – Joseph Silber Aug 17 '12 at 19:33
  • 1
    side note: `$.Deferred()` and `new $.Deferred()` are the same thing. the `new` is optional according to the documentation. – jbabey Aug 17 '12 at 19:44
  • This question should be updated so that it doesn't refer to the outdated documentation, but otherwise, this is still a useful question / answer thread. – Eric Elliott May 29 '14 at 15:42
  • related: [When should I use jQuery deferred's “then” method and when should I use the “pipe” method?](http://stackoverflow.com/q/9583783/1048572) – Bergi Aug 31 '14 at 14:38

1 Answers1

18

The documentation update for jQuery 1.8 is not online yet.

According to this recent blog post:

We’re in the process of updating the API documentation for all the changes to 1.8, but for now you can refer back to the changelog in the jQuery 1.8 announcement to see what’s changed.

Update: Yes, then() returns a new Deferred because it is equivalent to pipe() now. I'm pretty confident the documentation update will clarify this soon.

Further update for completeness: The documentation was recently updated and now says for pipe():

Deprecation Notice: As of jQuery 1.8, the deferred.pipe() method is deprecated. The deferred.then() method, which replaces it, should be used instead.

And for then():

Prior to jQuery 1.8, the arguments could be a function or an array of functions.

[...]

As of jQuery 1.8, the deferred.then() method returns a new promise that can filter the status and values of a deferred through a function, replacing the now-deprecated deferred.pipe() method. The doneFilter and failFilter functions filter the original deferred's resolved / rejected status and values. The progressFilter function filters any calls to the original deferred's notify or notifyWith methods. These filter functions can return a new value to be passed along to the promise's .done() or .fail() callbacks, or they can return another observable object (Deferred, Promise, etc) which will pass its resolved / rejected status and values to the promise's callbacks. If the filter function used is null, or not specified, the promise will be resolved or rejected with the same values as the original.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 4
    Unless I'm smoking crack, someone is, actually, updating the docs right now :) I was about to make a comment and then everything was different. – Jamie Treworgy Aug 17 '12 at 19:26
  • @Adam, and I'm afraid I have to close this question as [too localized](http://meta.stackexchange.com/q/4818/164403). Sorry, no harm meant. – Frédéric Hamidi Aug 17 '12 at 19:26
  • 4
    @FrédéricHamidi please don't - there's useful commentary to be made about the change. – Alnitak Aug 17 '12 at 19:27
  • @Alnitak, fair enough, but the question will have to be updated then (now it's all about doc inconsistencies). – Frédéric Hamidi Aug 17 '12 at 19:29
  • 4
    @FrédéricHamidi - Why in G-D's name would you consider this question "too localized"? – Joseph Silber Aug 17 '12 at 19:31
  • @Joseph, I answered under the question: Right now it's too tied to the docs, which are in the process of being updated, apparently as we speak. Refocusing the question can help. – Frédéric Hamidi Aug 17 '12 at 19:34
  • 2
    @FrédéricHamidi Very fair, I think "The docs are just wrong" is a great explanation as to, you know, why they _are wrong_! – Adam Terlson Aug 17 '12 at 19:35
  • @FrédéricHamidi Can you perhaps comment on my second question? Why does then() return a new deferred? Is that simply so you can make then() and pipe() actually be the same thing? – Adam Terlson Aug 17 '12 at 19:39
  • 1
    @Adam, yes, `then()` returns a new Deferred because `pipe()` does, and they're the same thing now. `pipe()` is a little more complicated than the old `then()`, I tried to explain it once in [this question](http://stackoverflow.com/q/10253192/464709). Maybe it can help. – Frédéric Hamidi Aug 17 '12 at 19:43
  • Of course, the documentation has been updated since this answer was submitted. Maybe an answer update is in order? – Eric Elliott May 29 '14 at 15:43