Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

refactor($q): separate Deferred out of Promise implementation #15064

Closed
wants to merge 1 commit into from

Conversation

jbedard
Copy link
Collaborator

@jbedard jbedard commented Aug 29, 2016

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
refactor / cleanup, maybe performance gains when creating promises

This makes Deferred a simple wrapper of Promise. Promises can now be constructed, chained etc without creating any Deferred objects.

Best viewed ignoring whitespace

@petebacondarwin petebacondarwin added this to the 1.5.9 milestone Aug 29, 2016
@petebacondarwin
Copy link
Contributor

I was thinking of something similar to this myself recently!


this.$$state.pending = this.$$state.pending || [];
this.$$state.pending.push([result, onFulfilled, onRejected, progressBack]);
if (this.$$state.status > 0) scheduleProcessQueue(this.$$state);

return result.promise;
return result;
},

'catch': function(callback) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was here before but why are catch and finally defined with strings and then is not?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are javascript keywords

Copy link

@aciccarello aciccarello Sep 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I should have realized that with catch.

@jbedard
Copy link
Collaborator Author

jbedard commented Sep 18, 2016

I've rebased this.

@gkalpak
Copy link
Member

gkalpak commented Sep 21, 2016

So, we are spared creating a Deferred object when constructing/chaining a Promise, which is not a bad thing. But we do lose the (indirect) ability to hook into Deferred.prototype (as @lgalfaso has mentioned elsewhere). So this is a potential breaking change (in practical terms).

I wonder to what extend was this hidden feature utilized. Maybe @lgalfaso has a better idea, since he said this has been explicitly discussed in the past.

@jbedard
Copy link
Collaborator Author

jbedard commented Sep 21, 2016

I can revert that part if really wanted, just seemed like something we can simplify along with everything else in this PR.

Note that what @lgalfaso is referring to is doing Object.getPrototypeOf($q.defer()).resolve = .... I don't think this is really a "supported" use case...

@lgalfaso
Copy link
Contributor

Just to be sure about my position. I think that this change is good and that it simplifies some of the existing code at $q. That said, I would only merge it in 1.6. The main reason is not only that this is a breaking change, but that there is no work around.

@gkalpak gkalpak modified the milestones: 1.6.0-rc.1, 1.5.9 Oct 5, 2016
@gkalpak
Copy link
Member

gkalpak commented Oct 5, 2016

(Adding it to the 1.6.0-rc.1 milestone, in order to consider it.)

@petebacondarwin
Copy link
Contributor

LGTM

@jbedard jbedard force-pushed the promise-standalone branch from a04bf03 to 342e27f Compare October 6, 2016 04:32
@jbedard
Copy link
Collaborator Author

jbedard commented Oct 6, 2016

Rebased again.

@petebacondarwin
Copy link
Contributor

Great. Can you add some information about the BC to the commit message and then we can merge.

BREAKING CHANGE:

The Deferred object returned by $q.defer() previously delegated methods to
those on Deferred.prototype. If Deferred.prototype was modified these
methods would reflect that.

This change removes that delegation and no longer uses methods on
Deferred.prototype.  Modifying methods on Deferred.prototype will no
longer have an effect.
@jbedard jbedard force-pushed the promise-standalone branch from 342e27f to 1ae9ae5 Compare October 7, 2016 03:41
@jbedard
Copy link
Collaborator Author

jbedard commented Oct 7, 2016

Adding a BC message.

@gkalpak gkalpak closed this in 34434cf Oct 7, 2016
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this pull request Oct 7, 2016
Closes angular#15064

BREAKING CHANGE:

Previously, the `Deferred` object returned by `$q.defer()` delegated the
`resolve()`, `reject()` and `notify()` methods to `Deferred.prototype`. Thus, it
was possible to modify `Deferred.prototype` and have the changes reflect to all
`Deferred` objects.

This commit removes that delegation, so modifying the above three methods on
`Deferred.prototype` will no longer have an effect on `Deferred` objects.
ellimist pushed a commit to ellimist/angular.js that referenced this pull request Mar 15, 2017
Closes angular#15064

BREAKING CHANGE:

Previously, the `Deferred` object returned by `$q.defer()` delegated the
`resolve()`, `reject()` and `notify()` methods to `Deferred.prototype`. Thus, it
was possible to modify `Deferred.prototype` and have the changes reflect to all
`Deferred` objects.

This commit removes that delegation, so modifying the above three methods on
`Deferred.prototype` will no longer have an effect on `Deferred` objects.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants