Promise: any vs race vs all in simple terms

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 promis…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Promise: any vs race vs all in simple terms." Kurapati Mahesh | Sciencx - Thursday May 18, 2023, https://www.scien.cx/2023/05/18/promise-any-vs-race-vs-all-in-simple-terms/
HARVARD
Kurapati Mahesh | Sciencx Thursday May 18, 2023 » Promise: any vs race vs all in simple terms., viewed ,<https://www.scien.cx/2023/05/18/promise-any-vs-race-vs-all-in-simple-terms/>
VANCOUVER
Kurapati Mahesh | Sciencx - » Promise: any vs race vs all in simple terms. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/05/18/promise-any-vs-race-vs-all-in-simple-terms/
CHICAGO
" » Promise: any vs race vs all in simple terms." Kurapati Mahesh | Sciencx - Accessed . https://www.scien.cx/2023/05/18/promise-any-vs-race-vs-all-in-simple-terms/
IEEE
" » Promise: any vs race vs all in simple terms." Kurapati Mahesh | Sciencx [Online]. Available: https://www.scien.cx/2023/05/18/promise-any-vs-race-vs-all-in-simple-terms/. [Accessed: ]
rf:citation
» Promise: any vs race vs all in simple terms | Kurapati Mahesh | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.