Ben Jenkinson
Ben Jenkinson

Reputation: 1834

How to make a jQuery Deferred object resolve/reject with the same 'resolved/rejected' state of another deferred?

I'm writing several functions that are effectively deferred objects that depend on varying combinations of other deferred objects.

function takesOneSecond() {
    return $.Deferred(function(deferred) {
        // Does something...
    }).promise();
}

function takesOneMinute() {
    return $.Deferred(function(deferred) {
        // Does something...
    }).promise();
}

function takesThreeMinutes() {
    return $.Deferred(function(deferred) {
        // Does something...
    }).promise();
}

function mySwitchingFunction() {

    return $.Deferred(function(deferred) {

        // Does something here..
        // Effectively chooses one of several other functions to call.

        if(/* choose 1 second */) {

            // We tie ourselves to the '1 second' function.

            // Call that function.
            takesOneSecond().done(function() {

                deferred.resolve(); // If that's done, I'm done too.

            }).fail(function() {

                deferred.reject(); // If that failed, I've failed too.

            });

        } else if(/* choose 1 minute */) {

            // Etc..

        } else if(/* choose 3 minutes */) {

            // Etc..

        }

    }).promise();

}

I'm writing this snippet of code a lot, is there no other way to make a deferred mirror or cascade the same 'resolved' or 'rejected' state of another deferred?

takesOneSecond().done(function() {
    deferred.resolve(); // If that's done, I'm done too.
}).fail(function() {
    deferred.reject(); // If that failed, I've failed too.
});

Upvotes: 4

Views: 3904

Answers (2)

Bergi
Bergi

Reputation: 664548

I think you don't need to construct a new promise at all. Just return the first promise.

function mySecondFunction() {
    // Does something here..
    // Effectively chooses one of several other functions to call.
    // In this case, assume I've just chosen the 'myFirstFunction' function.

    // Call that function and return its promise
    return myFirstFunction();
}

If you want to emphasize the "at the same time" part but maybe resolve with a different value, you could just create a new one by chaining with .then:

function mySecondFunction() {
    return myFirstFunction().then(function(resultOfFirst) {
        // but ignore it and
        return differentResult;
    }); // errors will propagate automatically
}

Upvotes: 1

dherman
dherman

Reputation: 2892

I think you may not understand promises. Using the .then method of a promise ( pipe in jQuery < 1.8 ), you can return a new promise and so on. That's how you build up a promise chain.

Here's an example of something that's similar to what you're trying to do:

function returnOne() {
  return $.Deferred(function( dfr ) {
     dfr.resolve( 1 );
  }).promise();
}

// Number will be the result of the original Deferred/Promise
function addTwo( num ) {
    return $.Deferred(function( dfr ) {
        dfr.resolve( num + 2 );
    }).promise();
}

returnOne().then( addTwo ).then(function( result ) {
    // Will be 3
    console.log( result );
});

Using that logic, you can filter the resolution or rejections of your promises however you want, including just re-resolving or rejecting with the same value, but maybe doing some intermediate work

Upvotes: 1

Related Questions