TypeScript Async Programming
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
awaiton an async helper: Withoutawait(or ESLint'sno-floating-promisesrule), rejected promises crash silently as unhandled rejections. - Sequential
awaitinside aforloop when requests are independent: Looping withawait fetch(...)runs requests one by one; map to promises and callPromise.all(or batch concurrency) for speed. - Using
asyncinsideArray.prototype.forEach():forEach()ignores returned promises and never awaits them; usefor ... offor sequential awaits orPromise.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.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.