Skip to main content

Lesson 3 of 7

Concurrency - Promise.all, race, allSettled

TypeScript preserves complete type information through all Promise combinators. `Promise.all` returns a typed tuple. `Promise.allSettled` returns an array of `PromiseSettledResult`. `Promise.race` and `Promise.any` return the first-resolved type. Understanding the type signatures of these combinators prevents race condition bugs.

11 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Promise Combinators - Fully Typed Concurrency

Promise Combinators - Fully Typed Concurrency
// ── Promise.all - run in parallel, wait for ALL ──────────────
// The return type is the tuple of all resolutions:
const [task, user, project] = await Promise.all([
  fetchTask(1),        // Promise<Task>
  fetchUser(1),        // Promise<User>
  fetchProject(1),     // Promise<Project>
]);
// task:    Task
// user:    User
// project: Project
// TypeScript infers tuple types from the array!

// Typed explicitly when needed:
const results: [Task[], User[]] = await Promise.all([
  fetchTasksByUser(1),
  fetchAllUsers(),
]);

// ── Promise.allSettled - run all, don't throw on failure ──────
// Every result is either fulfilled or rejected:
const settled = await Promise.allSettled([
  fetchTask(1),
  fetchTask(999),   // might fail
  fetchTask(2),
]);

// settled: PromiseSettledResult<Task>[]
// PromiseSettledResult<T> = PromiseFulfilledResult<T> | PromiseRejectedResult

for (const result of settled) {
  if (result.status === "fulfilled") {
    console.log("Got:", result.value.title);   // Task
  } else {
    console.error("Failed:", result.reason);   // unknown
  }
}

// Extract only fulfilled tasks:
const tasks = settled
  .filter(
    (r): r is PromiseFulfilledResult<Task> => r.status === "fulfilled"
  )
  .map((r) => r.value);  // Task[] ✅

// ── Promise.race - first to resolve or reject wins ─────────────
const TIMEOUT_MS = 5000;

async function withTimeout<T>(
  promise: Promise<T>,
  ms: number,
): Promise<T> {
  const timeout = new Promise<never>((_, reject) =>
    setTimeout(() => reject(new Error(`Timed out after ${ms}ms`)), ms)
  );
  return Promise.race([promise, timeout]);
}

const task = await withTimeout(fetchTask(1), TIMEOUT_MS);
// Type: Task - the never from timeout doesn't appear in T

// ── Promise.any - first to RESOLVE (ignores rejections) ───────
const fastestTask = await Promise.any([
  fetchFromPrimary(),    // Promise<Task>
  fetchFromReplica(),    // Promise<Task>
  fetchFromFallback(),   // Promise<Task>
]);
// fastestTask: Task  - whichever resolved first

Common Mistakes

  • Using `Promise.all` when failures should be isolated - if one promise rejects, `Promise.all` rejects immediately (fast-fail). Use `Promise.allSettled` when all results are needed regardless of individual failures.
  • Not filtering `allSettled` results - using `.map(r => r.value)` on allSettled results will give `undefined` for rejected items. Always filter with a type guard first.
  • Not knowing `Promise.race` type behavior with `never` - a `Promise<never>` in a `Promise.race` just means that branch can only reject, never resolve. TypeScript correctly omits it from the fulfillment type.

Tip

Tip

Practice Concurrency Promiseall race allSettled in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Concurrency - Promise.all, race, allSettled

Practice Task

Note

Practice Task - (1) Write a working example of Concurrency Promiseall race allSettled from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Concurrency Promiseall race allSettled is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.

Key Takeaways

  • TypeScript preserves complete type information through all Promise combinators.
  • Using `Promise.all` when failures should be isolated - if one promise rejects, `Promise.all` rejects immediately (fast-fail). Use `Promise.allSettled` when all results are needed regardless of individual failures.
  • Not filtering `allSettled` results - using `.map(r => r.value)` on allSettled results will give `undefined` for rejected items. Always filter with a type guard first.
  • Not knowing `Promise.race` type behavior with `never` - a `Promise<never>` in a `Promise.race` just means that branch can only reject, never resolve. TypeScript correctly omits it from the fulfillment type.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/7 done