0Pricing
JavaScript Academy · Lesson

Promise.all / any / race / allSettled — choose the right tool

Practice Promise combinators: all, any, race, allSettled. Learn success/failure behavior and when to use each one.

Combinators overview

Goal: Learn four helpers and their behavior.

  • all: wait for all; fail fast
  • any: first success
  • race: first settled (success/error)
  • allSettled: results for all
Promise.all / any / race / allSettled — choose the right tool — illustration 1

Promise.all basics

Promise.all resolves with an array of results; a single rejection rejects the whole thing.

// Small helpers for examples
function delay(ms, value, shouldFail = false) {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      if (shouldFail) reject(new Error(String(value)));
      else resolve(value);
    }, ms);
  });
}

// Promise.all waits for all; rejects fast on first error
Promise.all([
  delay(10, "A"),
  delay(5, "B")
]).then(function (vals) {
  console.log("all:", vals);
});
Promise.all / any / race / allSettled — choose the right tool — illustration 2

All lessons in this course

  1. Event Loop & Task vs Microtask Basics
  2. Promises — States, Chaining, Error Handling
  3. Promise.all / any / race / allSettled — choose the right tool
← Back to JavaScript Academy