Forem

Bosco Domingo
Bosco Domingo

Posted on • Edited on

1

The difference between Promise.all() vs Promise.allSettled() vs Promise.any() vs Promise.race() in 30 seconds

If you're writing code in JavaScript or TypeScript (or CoffeeScript for whatever reason!) you'll come across Promises. One of the best features they provide is concurrency, which is usually achieved with Promise.all(). That being said, I'd like to quickly explore the differences between the available methods: all(), allSettled(), any() and race() (this last one I hadn't even heard of until researching for this article!).

At a glance

All of them start the Promises concurrently (not necessarily in parallel!). They all ignore non-promises.

  • all(): Resolves only if all promises resolve. Early reject if any one promise rejects. Returns an array of the settled values in the order they were inserted, not settled. Promise.all() resolves synchronously if and only if the iterable passed is []. Even if you pass [1, 'abc', {}], it will resolve asynchronously.
  • allSettled(): Resolves only if all promises resolve. Reject if any one promise rejects (just like all() so far) BUT WAITS FOR ALL OTHERS TO FINISH. Returns an array of the settled status and values in the order they were inserted, not settled.
  • any(): Resolves if any promise resolves. Rejects only if all the promises reject (opposite to all()). Returns the first resolved value.
  • race(): Rejects or resolves according to whatever the first settled promise does. Returns the first resolved value.

Don't forget to check out all my other content for more guides like this one!


Here is some code you can test yourself to check the results. Feel free to test it out and comment any findings you may have!

Here is the GitHub repo too. Give it a star ⭐ while you're at it!

/* All of them start the Promises concurrently (not in parallel necessarily!). They all ignore non-promises.

- all(): Resolves only if all promises resolve. Early reject if any one promise rejects.
    Returns an array of the settled values in the order they were inserted, not settled.
    Promise.all() resolves synchronously if and only if the iterable passed is []. Even if you pass [1,'abc',{}], it will resolve asynchronously.

- allSettled(): Resolves only if all promises resolve. Reject if any one promise rejects BUT WAITS FOR ALL OTHERS TO FINISH.
    Returns an array of the settled stati and values in the order they were inserted, not settled.

- any(): Resolves if any promise resolves. Rejects only if all the promises reject (opposite to all()).
    Returns the first resolved value.

- race(): Rejects or resolves according to whatever the first settled promise does.
    Returns the first settled value.
*/
let immediateResolve: Promise<number>,
  nonPromise: number,
  timeoutPromise: Promise<string>;

function setPromises() {
  immediateResolve = Promise.resolve(3);
  nonPromise = 42;
  timeoutPromise = new Promise((resolve, _) => {
    setTimeout(resolve, 1000, "Resolved after timeout");
  });
}

async function tryPromiseAll() {
  setPromises();
  const result = await Promise.all([
    immediateResolve,
    nonPromise,
    timeoutPromise,
  ]);
  console.log("Promise.all 1: ", result); // Expected output (after at least 1000ms): Array [3, 42, "foo"]

  setPromises();
  try {
    const resultWithRejection = await Promise.all([
      immediateResolve,
      nonPromise,
      timeoutPromise,
      Promise.reject("Immediate reject"),
    ]);
    console.log("Promise.all 2: ", resultWithRejection); // Never reached, rejection causes interruption
  } catch (error) {
    console.error("Error:", error);
  }

  setPromises();
  try {
    const resultWithCaughtRejection = await Promise.all([
      immediateResolve,
      nonPromise,
      timeoutPromise,
      Promise.reject("Immediate reject").catch(() => {}),
    ]);
    console.log("Promise.all 3: ", resultWithCaughtRejection); // Expected: [ 3, 42, 'Resolved after timeout', undefined ]. Rejection is never bubbled up
  } catch (error) {
    console.error("Error:", error);
  }
}

async function tryPromiseAllSettled() {
  setPromises();
  const result = await Promise.allSettled([
    immediateResolve,
    nonPromise,
    timeoutPromise,
  ]);
  console.log("Promise.allSettled 1: ", result);
  // Expected output: [{ status: 'fulfilled', value: 3 }, { status: 'fulfilled', value: 42 }, { status: 'fulfilled', value: 'Resolved after timeout' }]

  setPromises();

  try {
    const resultWithRejection = await Promise.allSettled([
      immediateResolve,
      nonPromise,
      timeoutPromise,
      Promise.reject("Immediate reject"),
    ]);
    console.log("Promise.allSettled 2: ", resultWithRejection);
    // Expected output: [{ status: 'fulfilled', value: 3 }, { status: 'fulfilled', value: 42 }, { status: 'fulfilled', value: 'Resolved after timeout' }, { status: 'rejected', reason: 'Immediate reject' }]
  } catch (error) {
    console.error("Error:", error);
  }
}

async function tryPromiseAny() {
  setPromises();
  const result = await Promise.any([
    immediateResolve,
    nonPromise,
    timeoutPromise,
  ]);
  console.log("Promise.any 1: ", result); // Expected output: 3, not 2, as it doesn't come from a promise

  setPromises();

  try {
    const resultWithRejection = await Promise.any([
      immediateResolve,
      nonPromise,
      timeoutPromise,
      Promise.reject("Immediate reject"),
    ]);
    console.log("Promise.any 2: ", resultWithRejection); // Always reached unless they all rejected
  } catch (error) {
    console.error("Error:", error);
  }
}

async function tryPromiseRace() {
  setPromises();
  const result = await Promise.any([
    immediateResolve,
    nonPromise,
    timeoutPromise,
  ]);
  console.log("Promise.race 1: ", result); // Expected output: 3

  setPromises();

  try {
    const resultWithRejection = await Promise.any([
      immediateResolve,
      nonPromise,
      timeoutPromise,
      Promise.reject("Immediate reject"),
    ]);
    console.log("Promise.race 2: ", resultWithRejection); // Never reached
  } catch (error) {
    console.error("Error:", error);
  }
}

export default async function main(): Promise<void> {
  console.log("Starting test:\n");
  console.time("Promise.all");
  await tryPromiseAll();
  console.timeEnd("Promise.all");
  console.log("");

  console.time("Promise.allSettled");
  await tryPromiseAllSettled();
  console.timeEnd("Promise.allSettled");
  console.log("");

  console.time("Promise.any");
  await tryPromiseAny();
  console.timeEnd("Promise.any");
  console.log("");

  console.time("Promise.race");
  await tryPromiseRace();
  console.timeEnd("Promise.race");
}

main()
  .then(() => {
    process.exit(0);
  })
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
Enter fullscreen mode Exit fullscreen mode

Sources: all(), allSettled(), any(), race()

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay