question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Promise.race` use case?

See original GitHub issue

I’d love to hear what are some use cases to the more obscure functions like Promise.race 😃

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
spioncommented, Dec 25, 2013

@benjamingr I think not - the difference is that any will continue to wait for the successful fulfillment of one of the promises.

Basically:

.all - wait success of all, fail if just one

.some(arr, N) - wait success of N, fail when impossible to fulfill N (because arr.length - N + 1 have failed)

.any(arr) - wait for success of 1, fail when impossible to fulfill 1 (only when everyone fails)

.race(arr) - wait for 1 promise to succeed, fail if just a single one fails.

.settle(arr) - wait for all promises to settle (either with a value or with an error, doesnt matter)

2reactions
spioncommented, Dec 25, 2013

Sorry, I didn’t explain it very well. In a client side framework for example, I want the UI update to fail if the router’s route changes – there is no point in updating it anymore.

orRouteChangedFail() will fail if the route changes, causing the race to also fail.

Also,

Promise.any(someAction(), Promise.delay(1000)).then(value => ...)

will result with value == undefined – so if the action we’re waiting for also has no return results, we will be unable to distinguish the timeout from the successful completion of the action.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Promise.race() - JavaScript - MDN Web Docs
The Promise.race() method takes an iterable of promises as input and returns a single Promise. This returned promise settles with the ...
Read more >
Learn JavaScript Promise.race() By Practical Examples
The Promise.race() static method accepts a list of promises as an iterable object and returns a new promise that fulfills or rejects as...
Read more >
Understanding promise.race() usage - Stack Overflow
Promise.race is a JS built in function that accepts an iterable of Promises (e.g. Array ) as an argument. This function then asynchronously ......
Read more >
JavaScript Promise.race() Method - GeeksforGeeks
The Promise.race() method returns a promise that fulfills or rejects as soon as one of the promises in an iterable fulfills or rejects, ......
Read more >
JavaScript Promises: race, all, allSettled, and then
In this article, I want to cover the methods that'll help you deal with some more complex use cases, while also dealing with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found