Bug Tracker

Opened 7 years ago

Closed 6 years ago

#13717 closed feature (plugin)

$.Deferred helper methods to resolve/reject

Reported by: danheberden Owned by:
Priority: low Milestone: None
Component: deferred Version: 2.0b2
Keywords: Cc:
Blocked by: Blocking:

Description

TL;DR the only way to change a failure to a resolution and vice-versa is to return a new promise. This is different from the promises/A spec (for good reason, I feel) but still is cumbersome to work with. I propose either:

$.Deferred.resolve(value); // returns a new, done promise with value
$.Deferred.reject(value); // returns a new, failed promise with value

OR

since $.when already does this for synchronous content, create $.unless that takes any number args and only succeeds if all of them fail.

For details, implementation examples, and a fun time check out http://jsfiddle.net/danheberden/jhXAp/

xoxo, Dan

Change History (5)

comment:1 Changed 7 years ago by jaubourg

On my own projects, I have something as follows:

$.each( [ "resolve", "reject" ], function( methodWith, method ) {
    methodWith = method + "With";
    $[ method ] = function() {
        return $.Deferred()[ methodWith ]( this, arguments ).promise();
    };
} );

// Later on
promise.then( function( data )
    try {
        return doSomething( data );
    } catch( e ) {
        return $.reject( e );
    }
} );

Not sure if they should be in core but we could use some Deferred-related plugins for sure.

Not a fan of $.Deferred.resolve, I don't see the gain compared to $.Deferred().resolve.

comment:2 Changed 7 years ago by danheberden

Any thoughts on $.unless, jaubourg?

comment:3 Changed 7 years ago by danheberden

Also, my mistake for not noticing then would work with a deferred and not just a promise, so $.Deferred().resolve(value) would be ok, albeit still a bit verbose.

comment:4 Changed 7 years ago by timmywil

Component: unfileddeferred
Priority: undecidedlow
Status: newopen

comment:5 Changed 6 years ago by dmethvin

Resolution: plugin
Status: openclosed

I don't think we are looking to add features, let's focus on Promise and see how we can integrate it. This can be done as a plugin if desired.

Note: See TracTickets for help on using tickets.