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

feat($q): implement $q.race #14757

Closed
wants to merge 3 commits into from
Closed

feat($q): implement $q.race #14757

wants to merge 3 commits into from

Conversation

yihangho
Copy link
Contributor

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Implement $q.race

What is the current behavior? (You can also link to an open issue here)
$q.race is non-existent (#12929)

Does this PR introduce a breaking change?
Nope.

Please check if the PR fulfills these requirements

Other information:

Implement $q.race. $q.race takes in an array or hash of promises and
returns a promsie that is resolved as soon as the first promise is
resolved.

Closes #12929

Implement $q.race. $q.race takes in an array or hash of promises and
returns a promsie that is resolved as soon as the first promise is
resolved.

Closes angular#12929
expect(logStr()).toBe('error(hi)->reject(hi)');
});
});

Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably also test passing an object as the param, e.g. $q.race({a: promise1, b: promise2}).

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've added tests for object params.

Copy link
Member

Choose a reason for hiding this comment

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

Do we want to support this (considering it is not supported in native Promises)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I am not too sure too. I decided to also take care of objects as this is also the case for $q.all.

@petebacondarwin petebacondarwin modified the milestones: 1.5.7, 1.5.x Jun 15, 2016
@petebacondarwin
Copy link
Contributor

Other than a few minor questions, this LGTM

@gkalpak gkalpak modified the milestones: 1.5.7, 1.5.8 Jun 15, 2016
* Returns a promise that is resolved similarly as the first promise to resolve.
*
* @param {Array.<Promise>|Object.<Promise>} promises An array or hash of promises.
* @returns {Promise} Returns a promise that will be resolved as soon as the first promise in
Copy link
Member

Choose a reason for hiding this comment

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

This description is a little missleading. It gives the impression we are looking for the first promise to actually resolve (not reject).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about something like this:

Returns a promise that will be settled as soon as the first promise in `promises` is settled. The value or reason will be the same as that of the first settled promise.

Note that the term settled is also used in the spec.

Copy link
Member

Choose a reason for hiding this comment

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

I just realized that we are using resolved inconsistently in the docs (either as fulfilled or resolved in the sense of the spec) 😒 (TBH, I myself thought resolved means fulfilled until now...)

I would suggest being a little more explicit:

Returns a promise that will be settled (i.e. fulfilled or rejected)...

Don't forget to also update the description above 😇

var deferred = defer();

forEach(promises, function(promise) {
when(promise).then(deferred.resolve, deferred.reject);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gkalpak Does this look good to you? (Notice I changed the initialization of deferred from new Deferred() to defer(). The reason is the the resolve and reject methods are bounded if the object is coming from defer().)

Copy link
Member

Choose a reason for hiding this comment

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

I didn't realize you were using new Deferred() before. I think it's fine either way. Leaving it up to you!

@petebacondarwin
Copy link
Contributor

LGTM - merging

@petebacondarwin
Copy link
Contributor

tweaked the docs description to match that on MDN

thanks @yihangho

petebacondarwin pushed a commit that referenced this pull request Jun 17, 2016
Implement $q.race. $q.race takes in an array or hash of promises and
returns a promise that resolves or rejects as soon as one of those promises
resolves or rejects, with the value or reason from that promise.

Closes #12929
Closes #14757
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.

$q.race() method to match ES6
5 participants