Bug Tracker

Opened 10 years ago

Closed 9 years ago

#14226 closed feature (notabug)

Provide Deferred Instance Detection

Reported by: [email protected] Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.10.2
Keywords: Cc:
Blocked by: Blocking:


A Deferred object should (ideally) provide a mechanism for detection that it's a deferred promise. For instance, take code that calls a callback which may or may not return a deferred promise:

var result = someCallback();
if(result.isDeferred) {
    result.done(function() { // ... } });
} else if(result) {
    // ...

Currently, the following fails (for obvious reasons):

var f = new jQuery.Deferred();
if(f instanceof $.Deferred) {
    // ...

I would suggest a simple boolean on the object itself. I realize this is no better than duck type detection of the relevant method (typeof result.done === 'function'), but it is syntactically cleaner and easier to read.

Just a thought -- thanks!

Change History (3)

comment:1 Changed 10 years ago by dcherman

The Promise/A spec ( which I think is the one Deferreds conforms to ) defines a promise as an object which has a then property that is a function, so the test for a promise is really just typeof obj.then === "function"


Also, rather than doing your if statement, you should consider using $.when so that you can handle that return value in a consistent manner whether it's a promise or not.

comment:2 Changed 10 years ago by anonymous

Awesome! Thanks for elaborating on the spec -- that definitely makes sense. Also, I wasn't aware $.when operated as such -- thanks!

comment:3 Changed 9 years ago by dmethvin

Resolution: notabug
Status: newclosed

Agreed, it's best to use the standard check for a .then method.

Note: See TracTickets for help on using tickets.