Skip to main content

Bug Tracker

Side navigation

#14293 closed feature (wontfix)

Opened August 24, 2013 03:34PM UTC

Closed October 18, 2013 02:38PM UTC

Last modified October 18, 2013 09:40PM UTC

Deferreds and Promises should use a prototype

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

Deferreds and Promises should have a prototype so plugins can extend them and add functions.

I guess using a prototype would even make jQuery more lightweight because functions like then or always, … don't need to be redefined for each new Deferred.

As an example: imagine a function that triggers if the Deferred isn't resolved after a given timeout (e.g. to show a loading text only after something loads for more than 1s already)

http://jsfiddle.net/kDYPy/1/

Other adventages would be being able to check if an object is a deferred/promise using instanceof

this is similar to http://bugs.jquery.com/ticket/9937 i guess

note: http://bugs.jquery.com/ticket/9937#comment:4 says

"'the behaviour is exactly as expected and changing it here will break stuff'

No-one is proposing to move methods to Deferred.prototype, if that was your concern."

Well I'm proposing exactly that, but my guess is, that it wouldn't break anything.

Attachments (0)
Change History (5)

Changed September 10, 2013 07:20PM UTC by dmethvin comment:1

cc: → jaubourg

I think we intentionally designed the promise so that it could augment an existing object rather than inherit from a prototype.

Putting out the jaubourg-shaped signal light for further discussion.

Changed September 10, 2013 10:40PM UTC by jaubourg comment:2

Well, as long as the idea is that those methods would only be on the Deferred object and NOT on the Promise, we could have a Deferred use the prototype of $.Deferred. You would never be able to override default methods because they're on the object though.

As for determining if an object is a promise, we don't really have that in jQuery, you rather test if an object is "observable through a promise". So you test for the presence of a method called promise and call it to get the actual promise.

If you want to add methods on promises, then create a new type of object and call the promise method of a Deferred passing an instance of that object.

Example:

function MyPromise() {
}

MyPromise.prototype = {
  myAlways: function( fn ) {
    return this.done( fn ).fail( fn );
  }
};

var myPromise = Deferred().promise( new MyPromise() );

myPromise.myAlways( fn1 ).myAlways( fn2 );

myPromise instanceof MyPromise; // true

This works because promise and deferred methods chain on their context.

Changed October 18, 2013 02:38PM UTC by timmywil comment:3

resolution: → wontfix
status: newclosed

Changed October 18, 2013 09:08PM UTC by fastfasterfastest comment:4

>As for determining if an object is a promise, we don't really have that in jQuery, you rather test if an object is "observable through a promise". So you test for the presence of a method called promise and call it to get the actual promise.

I believe the presence of the promise method on promise objects is currently not documented - it is only documented that the promise method exists on deferred objects. So I filed a documentation bug for this at https://github.com/jquery/api.jquery.com/issues/375

Changed October 18, 2013 09:40PM UTC by jaubourg comment:5

I believe the presence of the promise method on promise objects is currently not documented - it is only documented that the promise method exists on deferred objects. So I filed a documentation bug for this at https://github.com/jquery/api.jquery.com/issues/375

Nice catch. I didn't realize that.