Skip to main content

Bug Tracker

Side navigation

#14226 closed feature (notabug)

Opened August 06, 2013 05:40PM UTC

Closed August 09, 2013 12:32AM UTC

Provide Deferred Instance Detection

Reported by: skoneberg@gmail.com Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.10.2
Keywords: Cc:
Blocked by: Blocking:
Description

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!

Attachments (0)
Change History (3)

Changed August 06, 2013 07:55PM UTC by dcherman comment:1

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"

http://wiki.commonjs.org/wiki/Promises/A

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.

Changed August 06, 2013 08:50PM UTC by anonymous comment:2

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

Changed August 09, 2013 12:32AM UTC by dmethvin comment:3

resolution: → notabug
status: newclosed

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