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

feat($q): add streamlined ES6-style interface for using $q #8311

Closed
wants to merge 1 commit into from

Conversation

caitp
Copy link
Contributor

@caitp caitp commented Jul 23, 2014

This potentially helps lead the way towards a more performant fly-weight implementation, as discussed earlier in the year. Using a constructor means we can put things in the prototype chain, and essentially treat $q as a Promise class, and reuse methods as appropriate.

Short of that, I feel this style is slightly more convenient and streamlined, compared with the older API.

(#8067 originally contained this PR, but has been refactored into a staging branch for the remaining changes left in #8300 --- This should be much easier to review as the first of a set of 3 changes).

@mary-poppins
Copy link

Thanks for the PR! Please check the items below to help us merge this faster. See the contributing docs for more information.

  • Uses the issue template (#8311)

If you need to make changes to your pull request, you can update the commit with git commit --amend.
Then, update the pull request with git push -f.

Thanks again for your help!

@caitp
Copy link
Contributor Author

caitp commented Jul 23, 2014

@jeffbcross I've stuck this chunk of the $q CL here to be reviewed separately, in case step 2 isn't ready tomorrow. Can you take a quick review at it?

It does still contain the duplicated set of tests, which are irrelevant for this CL, but if the next one lands, it will be very relevant, so I think it would be good to keep them. I'll remove them if that's too much of a problem though.

@btford btford added this to the 1.3.0-beta.17 milestone Jul 23, 2014
@jeffbcross jeffbcross self-assigned this Jul 23, 2014

$Q.defer = defer;
$Q.reject = reject;
$Q.when = when;
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be $Q.resolve?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, wait, I see. You're using the same constructor to power the new and old $q

Copy link
Contributor

Choose a reason for hiding this comment

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

I at first thought this was intended to match semantics of ES6 Promise.reject and Promise.resolve

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see a problem with adding a resolve() or resolved() helper too, but I'm trying to keep this one as simple as possible

@caitp caitp added cla: yes and removed cla: no labels Jul 23, 2014
* $q can be used in two fashions --- One, which is more similar to Kris Kowal's Q or jQuery's Deferred
* implementations, as well as a new ES6-inspired usage.
*
* The streamlined ES6 style promise can be used like so:
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be a little more specific about the extent of ES6-ness. I.e. the constructor+resolver function is the only thing brought in.

@jeffbcross
Copy link
Contributor

Other than one doc nitpick, LGTM

This potentially helps lead the way towards a more performant fly-weight implementation, as discussed
earlier in the year. Using a constructor means we can put things in the prototype chain, and essentially
treat $q as a Promise class, and reuse methods as appropriate.

Short of that, I feel this style is slightly more convenient and streamlined, compared with the older
API.
@caitp
Copy link
Contributor Author

caitp commented Jul 24, 2014

Okay, the docs have been reworked: http://ci.angularjs.org/job/angular.js-caitlin/426/artifact/build/docs/api/ng/service/$q

If that looks good, I think it can land.

I do think that the docs for this section might be re-organized further, but that's a separate issue.

@jeffbcross
Copy link
Contributor

I like the docs change, LGTM to merge

@caitp
Copy link
Contributor Author

caitp commented Jul 24, 2014

okay, will do

@caitp caitp closed this in f3a763f Jul 24, 2014
@jimmywarting
Copy link
Contributor

curious to see a benchmark... constructor vs property

@caitp
Copy link
Contributor Author

caitp commented Jul 26, 2014

We don't have an exact benchmark of refactored $q vs old $q (because making that work seems hard) --- but there is this: http://jsperf.com/angular-q-vs-bluebird2/2 (refactored) vs http://jsperf.com/angular-q-vs-bluebird2 (not refactored) --- note the difference

@rayshan
Copy link

rayshan commented Aug 10, 2014

Thanks for this, looking forward to future promise improvements.

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