Bug Tracker

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#14293 closed feature (wontfix)

Deferreds and Promises should use a prototype

Reported by: ppp1pp1p1@… 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.

Change History (5)

comment:1 Changed 6 years ago by dmethvin

Cc: jaubourg added

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.

comment:2 Changed 6 years ago by jaubourg

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.

comment:3 Changed 6 years ago by timmywil

Resolution: wontfix
Status: newclosed

comment:4 Changed 6 years ago by fastfasterfastest

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

comment:5 in reply to:  4 Changed 6 years ago by jaubourg

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.

Note: See TracTickets for help on using tickets.