Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen should I use jQuery deferred's "then" method and when should I use the "pipe" method?
    primarykey
    data
    text
    <p>jQuery's <a href="http://api.jquery.com/category/deferred-object/" rel="noreferrer"><code>Deferred</code></a> has two functions which can be used to implement asynchronous chaining of functions:</p> <h1><a href="http://api.jquery.com/deferred.then/" rel="noreferrer"><code>then()</code></a></h1> <blockquote> <p><strong><code>deferred.then( doneCallbacks, failCallbacks ) Returns: Deferred</code></strong> </p> <p><strong>doneCallbacks</strong> A function, or array of functions, called when the Deferred is resolved.<br> <strong>failCallbacks</strong> A function, or array of functions, called when the Deferred is rejected.</p> </blockquote> <h1><a href="http://api.jquery.com/deferred.pipe/" rel="noreferrer"><code>pipe()</code></a></h1> <blockquote> <p><strong><code>deferred.pipe( [doneFilter] [, failFilter] ) Returns: Promise</code></strong> </p> <p><strong>doneFilter</strong> An optional function that is called when the Deferred is resolved.<br> <strong>failFilter</strong> An optional function that is called when the Deferred is rejected.</p> </blockquote> <p>I know <code>then()</code> has been around a little longer than <code>pipe()</code> so the latter must add some extra benefit, but what the difference precisely is eludes me. Both take pretty much the same callback parameters though they differ in name and the difference between returning a <code>Deferred</code> and returning a <code>Promise</code> seems slight.</p> <p>I've read the official docs over and over but always find them too "dense" to really wrap my head around and searching has found lots of discussion of the one feature or the other but I haven't found anything that really clarifies the different pros and cons of each.</p> <p><strong>So when is it better to use <code>then</code> and when is it better to use <code>pipe</code>?</strong></p> <hr> <h1>Addition</h1> <p><a href="https://stackoverflow.com/a/9601230/527702">Felix's excellent answer</a> has really helped clarify how these two functions differ. But I wonder if there are times when the functionality of <code>then()</code> is preferable to that of <code>pipe()</code>.</p> <p>It is apparent that <code>pipe()</code> is more powerful than <code>then()</code> and it seems the former can do anything the latter can do. One reason to use <code>then()</code> might be that its name reflects its role as the termination of a chain of functions processing the same data.</p> <p><strong>But is there a use case that requires <code>then()</code>'s returning the original <code>Deferred</code> that can't be done with <code>pipe()</code> due to it returning a new <code>Promise</code>?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload