What is the difference between Promise race and any methods with examples
- Admin
- Dec 3, 2023
- Javascript
A promise is a javascript object used for asynchronous operations. It is used to execute future jobs that contain status pending: Default initial state fulfilled: The task is done successfully with a value rejected: task failed with a reason It is introduced since ES6 Version in javascript.
Promise any example in javascript
Promise any() method accepts iterable promises and returns results below
- Result is Resolved promise when one of the array promises is resolved
- Result is AggregateError when all promises are rejected.
Promise.any(iterable promises)
Iterable Promises are an array of Promises.
It returns resolved promise as soon as one of the promises is resolved.
If all promises are rejected, It throws AggregateError: All promises were rejected
Let’s see different use cases
- Multiple all promises are resolved
Promise. any returns the first promise resolved when multiple promises are resolved
Promise.any([
Promise.resolve("p1 success"),
Promise.resolve("p2 success"),
Promise.resolve("p3 success")])
.then((data) => console.log("result", data))
.catch((error) => console.log("error", error))
It returns the first resolved promise Output:
result p1 success
- One of the promises is resolved
Promise.any([
Promise.reject("p1 failed"),
Promise.resolve("p2 success"),
Promise.reject("p3 failed")])
.then((data) => console.log("result", data))
.catch((error) => console.log("error", error))
Output:
result p2 success
- All the promises are rejected
all method takes three promises. One promise is rejected, 2 processes are resolved and the result is failure
Promise.any([Promise.reject("p1 failed"), Promise.reject("p2 failed"),
Promise.reject("p3 failed")
])
.then((data) => console.log("result", data)).catch((error) =>
console.log("error", error))
error
AggregateError: All promises were rejected
Promise race() example in javascript
Promise.race accepts iterable promises and returns one of the promised result as soon as one of the promises is fulfilled or rejected
All the promises are settled with either reject or resolved and returns object
if the promise is fulfilled, Returns the object of status=fulfilled
and value
If the promise is rejected, Returns the object of status=rejected
and reason
Promise.all(iterable promises)
- All promises are resolved In this example, all promises are resolved
Promise.race([
Promise.resolve("p1 success"),
Promise.resolve("p2 success"),
Promise.resolve("p3 success")])
.then((data) => console.log("result", data))
.catch((error) => console.log("error", error)) ```
Output:
```markdown
result p1 success
```
- One of the promises is rejected
In this example one of the promises is rejected, remaining promises are resolved
Promise.race([
Promise.resolve("p1 success"),
Promise.reject("p2 failed"),
Promise.resolve("p3 success")
])
.then((data) => console.log(data))
.catch((error) => console.log("error", error))
Output:
p1 success
- All promises are rejected
Promise.race([
Promise.reject("p1 failed"),
Promise.reject("p2 failed"),
Promise.reject("p3 failed")
])
.then((data) => console.log(data))
.catch((error) => console.log("error", error))
Output:
error p1 failed
What is the difference between Promise race () and Promise any ()?
Promise race | Promise any |
---|---|
Takes Iterable array promises | Takes Iterable array promises |
Settled when any of the promises is fulfilled or rejected | Settled when any promise is resolved or all promises are rejected |
Both are not parallel | not parallel |