Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7621111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:06:54+00:00 2026-05-31T04:06:54+00:00

jQuery’s Deferred has two functions which can be used to implement asynchronous chaining of

  • 0

jQuery’s Deferred has two functions which can be used to implement asynchronous chaining of functions:

then()

deferred.then( doneCallbacks, failCallbacks ) Returns: Deferred

doneCallbacks A function, or array of functions, called when the Deferred is resolved.
failCallbacks A function, or array of functions, called when the Deferred is rejected.

pipe()

deferred.pipe( [doneFilter] [, failFilter] ) Returns: Promise

doneFilter An optional function that is called when the Deferred is resolved.
failFilter An optional function that is called when the Deferred is rejected.

I know then() has been around a little longer than pipe() 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 Deferred and returning a Promise seems slight.

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.

So when is it better to use then and when is it better to use pipe?


Addition

Felix’s excellent answer has really helped clarify how these two functions differ. But I wonder if there are times when the functionality of then() is preferable to that of pipe().

It is apparent that pipe() is more powerful than then() and it seems the former can do anything the latter can do. One reason to use then() might be that its name reflects its role as the termination of a chain of functions processing the same data.

But is there a use case that requires then()‘s returning the original Deferred that can’t be done with pipe() due to it returning a new Promise?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:06:55+00:00Added an answer on May 31, 2026 at 4:06 am

    Since jQuery 1.8 .then behaves the same as .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

    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 examples below might still be helpful to some.


    They serve different purposes:

    • .then() is to be used whenever you want to work with the result of the process, i.e. as the documentation says, when the deferred object is resolved or rejected. It is the same as using .done() or .fail().

    • You’d use .pipe() to (pre)filter the result somehow. The return value of a callback to .pipe() will be passed as argument to the done and fail callbacks. It can also return another deferred object and the following callbacks will be registered on this deferred.

      That is not the case with .then() (or .done(), .fail()), the return values of the registered callbacks are just ignored.

    So it is not that you use either .then() or .pipe(). You could use .pipe() for the same purposes as .then() but the converse does not hold.


    Example 1

    The result of some operation is an array of objects:

    [{value: 2}, {value: 4}, {value: 6}]
    

    and you want to compute the minimum and maximum of the values. Lets assume we use two done callbacks:

    deferred.then(function(result) {
        // result = [{value: 2}, {value: 4}, {value: 6}]
    
        var values = [];
        for(var i = 0, len = result.length; i < len; i++) {
            values.push(result[i].value);
        }
        var min = Math.min.apply(Math, values);
    
       /* do something with "min" */
    
    }).then(function(result) {
        // result = [{value: 2}, {value: 4}, {value: 6}]
    
        var values = [];
        for(var i = 0, len = result.length; i < len; i++) {
            values.push(result[i].value);
        }
        var max = Math.max.apply(Math, values);
    
       /* do something with "max" */ 
    
    });
    

    In both cases you have to iterate over the list and extract the value from each object.

    Wouldn’t it be better to somehow extract the values beforehand so that you don’t have to do this in both callbacks individually? Yes! And that’s what we can use .pipe() for:

    deferred.pipe(function(result) {
        // result = [{value: 2}, {value: 4}, {value: 6}]
    
        var values = [];
        for(var i = 0, len = result.length; i < len; i++) {
            values.push(result[i].value);
        }
        return values; // [2, 4, 6]
    
    }).then(function(result) {
        // result = [2, 4, 6]
    
        var min = Math.min.apply(Math, result);
    
        /* do something with "min" */
    
    }).then(function(result) {
        // result = [2, 4, 6]
    
        var max = Math.max.apply(Math, result);
    
        /* do something with "max" */
    
    });
    

    Obviously this is a made up example and there are many different (maybe better) ways to solve this problem, but I hope it illustrates the point.


    Example 2

    Consider Ajax calls. Sometimes you want to initiate one Ajax call after a previous one completes. One way is to make the second call inside a done callback:

    $.ajax(...).done(function() {
        // executed after first Ajax
        $.ajax(...).done(function() {
            // executed after second call
        });
    });
    

    Now lets assume you want to decouple your code and put these two Ajax calls inside a function:

    function makeCalls() {
        // here we return the return value of `$.ajax().done()`, which
        // is the same deferred object as returned by `$.ajax()` alone
    
        return $.ajax(...).done(function() {
            // executed after first call
            $.ajax(...).done(function() {
                // executed after second call
            });
        });
    }
    

    You’d like to use the deferred object to allow other code which calls makeCalls to attach callbacks for the second Ajax call, but

    makeCalls().done(function() {
        // this is executed after the first Ajax call
    });
    

    would not have the desired effect as the second call is made inside a done callback and not accessible from the outside.

    The solution would be to use .pipe() instead:

    function makeCalls() {
        // here we return the return value of `$.ajax().pipe()`, which is
        // a new deferred/promise object and connected to the one returned
        // by the callback passed to `pipe`
    
        return $.ajax(...).pipe(function() {
            // executed after first call
            return $.ajax(...).done(function() {
                // executed after second call
            });
        });
    }
    
    makeCalls().done(function() {
        // this is executed after the second Ajax call
    });
    

    By using .pipe() you can now make it possible to append callbacks to the "inner" Ajax call without exposing the actual flow/order of the calls.


    In general, deferred objects provide an interesting way to decouple your code 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Jquery has utility a function called ' extend ' which can be used to
Jquery has a great language construct that looks like this: $(document).ready(function() { $(a).click(function() {
jQuery's getScript function doesn't seem to support an error callback function. I can't use
I have a jquery bug and I've been looking for hours now, I can't
I used javascript for loading a picture on my website depending on which small
jQuery's slideUp and slideDown don't seem to do anything with selects. I can wrap
jQuery's deferred's have never liked me, and I've never been fully able to understand
jQuery 1.7.1, tablesorter, IE 8 - My table has been enabled for sorting using
jQuery 1.3.2 minified is 55.9K, and jQuery UI core itself has more than 110K
JQuery's sortable has an option to revert the item that the user drags back

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.