Opened 10 years ago
Closed 9 years ago
#14226 closed feature (notabug)
Provide Deferred Instance Detection
Reported by: | 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!
Change History (3)
comment:1 Changed 10 years ago by
comment:2 Changed 10 years ago by
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
Resolution: | → notabug |
---|---|
Status: | new → closed |
Agreed, it's best to use the standard check for a .then
method.
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 justtypeof 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.