TypeScript Academy · Lesson

Concurrency patterns (all/settled/race) types

Model concurrency with Promise.all / allSettled / race, understand their result types, and build safe helpers.

Lesson 3 of 38 steps

Concurrency patterns (all/settled/race) types is a free TypeScript Academy lesson on CoddyKit. This is lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 3 lessons in total.

Intro

Goal: Run tasks concurrently with Promise.all, handle mixed outcomes via allSettled, and use race for timeouts/fallbacks. Learn the types each returns.

all: tuple types

Promise.all with a tuple preserves element types and order; each element is awaited to its inner type.

async function one(): Promise<number> { return 1 }
async function two(): Promise<string> { return "two" }

async function runAll() {
  const [a, b] = await Promise.all([one(), two()]);
  // a: number, b: string
  console.log(a + 1, b.toUpperCase());
}

runAll();

all: concurrent map

Kick off multiple tasks first, then await them together. Promise.all yields string[] here.

async function fetchItem(id: number): Promise<string> {
  return `item-${id}`;
}

async function getMany(ids: number[]): Promise<string[]> {
  const tasks = ids.map(id => fetchItem(id)); // start all
  return Promise.all(tasks); // await all
}

getMany([1,2,3]).then(xs => console.log(xs));

allSettled typing

allSettled never rejects; it resolves to an array of fulfilled or rejected result objects you can narrow by status.

async function mightFail(n: number): Promise<number> {
  if (n % 2 === 0) return n; else throw new Error("odd")
}

async function runSettled() {
  const results = await Promise.allSettled([mightFail(1), mightFail(2)]);
  for (const r of results) {
    if (r.status === "fulfilled") {
      // r: PromiseFulfilledResult<number>
      console.log("ok", r.value);
    } else {
      // r: PromiseRejectedResult
      console.warn("fail", r.reason);
    }
  }
}

runSettled();

race + timeout

race resolves/rejects with the first settled promise. Combine with a timeout to cap latency.

function timeout<T>(ms: number, msg = "Timeout"): Promise<T> {
  return new Promise((_, reject) => setTimeout(() => reject(new Error(msg)), ms));
}

async function slow(): Promise<string> { return new Promise(res => setTimeout(() => res("done"), 100)); }

async function withTimeout() {
  const result = await Promise.race<Promise<string>>([
    slow(),
    timeout<string>(50, "Too slow"),
  ]);
  console.log(result);
}

withTimeout().catch(e => console.error("raced:", e.message));

Tips & choices

Tips:

  • all: fail-fast gathering of results.
  • allSettled: collect successes and errors without throwing.
  • race: timeouts/fallbacks; be explicit about the element types.

Promise.all typing check

Quick check: Which statement about Promise.all typing is TRUE?

Recap

Recap: Use all for typed parallelism, allSettled to inspect all outcomes, and race for timeouts—know the shapes each API returns.

Free to start

Learn TypeScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
101
Lessons
352

Frequently Asked Questions

Is the “Concurrency patterns (all/settled/race) types” lesson free?

Yes — the full text of “Concurrency patterns (all/settled/race) types” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO. The TypeScript Academy course includes 3 lessons in total.

What will I learn in “Concurrency patterns (all/settled/race) types”?

Model concurrency with Promise.all / allSettled / race, understand their result types, and build safe helpers. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.

How long does the “Concurrency patterns (all/settled/race) types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Promise , async/await typing
  2. Error typing (unknown), narrowing in catch
  3. Concurrency patterns (all/settled/race) types
← Back to TypeScript Academy