This content originally appeared on DEV Community and was authored by Kurapati Mahesh
In short:
Promise.all: Returns all resolved values or first rejected.
Promise.any: Returns first resolved or aggregated all errors
Promise.race: Returns first resolved or rejected
Example1:
const promise1 = Promise.resolve("Hello");
const promise2 = Promise.resolve("World");
const promise3 = Promise.resolve("!");
Promise.all([promise1, promise2, promise3])
.then((results) => console.log("Success:" + results))
.catch((error) => console.log("Error:" + error));
> Success:Hello,World,!
Promise.any([promise1, promise2, promise3])
.then((results) => console.log("Success:" + results))
.catch((error) => console.log("Error:" + error));
> Success:Hello
Promise.race([promise1, promise2, promise3])
.then((results) => console.log("Success:" + results))
.catch((error) => console.log("Error:" + error));
> Success:Hello
Example2:
const promise1 = Promise.resolve("Hello");
const promise2 = Promise.resolve("World");
const promise3 = Promise.reject("!");
Promise.all([promise1, promise2, promise3])
.then((results) => console.log("Success:" + results))
.catch((error) => console.log("Error:" + error));
> Error:!
Promise.any([promise1, promise2, promise3])
.then((results) => console.log("Success:" + results))
.catch((error) => console.log("Error:" + error));
> Success:Hello
Promise.race([promise1, promise2, promise3])
.then((results) => console.log("Success:" + results))
.catch((error) => console.log("Error:" + error));
> Success:Hello
Example3:
const promise1 = Promise.reject("Hello");
const promise2 = Promise.resolve("World");
const promise3 = Promise.resolve("!");
Promise.all([promise1, promise2, promise3])
.then((results) => console.log("Success:" + results))
.catch((error) => console.log("Error:" + error));
> Error:Hello
Promise.any([promise1, promise2, promise3])
.then((results) => console.log("Success:" + results))
.catch((error) => console.log("Error:" + error));
> Success:World
Promise.race([promise1, promise2, promise3])
.then((results) => console.log("Success:" + results))
.catch((error) => console.log("Error:" + error));
> Error:Hello
Example4:
const promise1 = Promise.reject("Hello");
const promise2 = Promise.reject("World");
const promise3 = Promise.reject("!");
Promise.all([promise1, promise2, promise3])
.then((results) => console.log("Success:" + results))
.catch((error) => console.log("Error:" + error));
> Error:Hello
Promise.any([promise1, promise2, promise3])
.then((results) => console.log("Success:" + results))
.catch((error) => console.log("Error:" + error));
> Error:AggregateError: All promises were rejected
Promise.race([promise1, promise2, promise3])
.then((results) => console.log("Success:" + results))
.catch((error) => console.log("Error:" + error));
> Error:Hello
Please do add any more scenarios you came across.
Thanks.
This content originally appeared on DEV Community and was authored by Kurapati Mahesh
Kurapati Mahesh | Sciencx (2023-05-18T13:43:22+00:00) Promise: any vs race vs all in simple terms. Retrieved from https://www.scien.cx/2023/05/18/promise-any-vs-race-vs-all-in-simple-terms/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.