`Promise.race` use case?
See original GitHub issueI’d love to hear what are some use cases to the more obscure functions like Promise.race
😃
Issue Analytics
- State:
- Created 10 years ago
- Comments:10 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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)
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,
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.