Lesson

TypeScript Async Programming

10 min reading
Free Course

TypeScript Async Programming: Promises, async/await, and Concurrency

Asynchronous TypeScript combines JavaScript Promise execution with generic Promise<T> return types so your editor knows the exact shape of resolved data after await. Every async function automatically wraps its return value inside a Promise<T>.

Typing async boundaries stops common bugs like forgetting await (passing a Promise object where a string was expected) or mishandling failed network calls.

Typing async/await and Parallel Requests

Use Promise.all([...]) to run independent async calls concurrently instead of awaiting them one by one, and let TypeScript infer the tuple of resolved types automatically.

interface TaskItem {
  id: number;
  done: boolean;
}

async function fetchTask(id: number): Promise<TaskItem> {
  return Promise.resolve({ id, done: id % 2 === 0 });
}

async function loadDashboard(): Promise<void> {
  const [first, second] = await Promise.all([fetchTask(1), fetchTask(2)]);
  console.log(first, second);
}

loadDashboard();

Handling Errors and Partial Failures

While Promise.all() rejects immediately if any single promise fails, Promise.allSettled() waits for every task and returns a discriminated union (status: "fulfilled" vs status: "rejected") so you inspect each outcome safely.

Common Mistakes to Avoid

  • Forgetting await on an async helper: Without await (or ESLint's no-floating-promises rule), rejected promises crash silently as unhandled rejections.
  • Sequential await inside a for loop when requests are independent: Looping with await fetch(...) runs requests one by one; map to promises and call Promise.all (or batch concurrency) for speed.
  • Using async inside Array.prototype.forEach(): forEach() ignores returned promises and never awaits them; use for ... of for sequential awaits or Promise.all(items.map(...)) for parallel work.

Frequently Asked Questions

What does Awaited<T> do in TypeScript?

Awaited<Promise<User>> unwraps a Promise type down to User, useful when typing wrapper functions around async helpers.

How do I type a fetch() JSON response safely?

response.json() returns Promise<any> in standard DOM types. Wrap it in a generic helper that validates with Zod or checks fields before returning Promise<T>.

What return type should an async function with no return use?

Write Promise<void>—TypeScript rejects bare void as the return type of an async function.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
TypeScript Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum