Promise<T>, async/await typing
Understand Promise shapes, annotate async functions, and work with awaited values safely.
Promise<T>, async/await typing is a free TypeScript Academy lesson on CoddyKit — lesson 1 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, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Goal: Read and write Promise<T>-based code. You will type async functions, await values, and keep errors typed.
Promise basics
Promise<T> wraps an eventual value. Use Promise when no value resolves.
function delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
const p: Promise<void> = delay(100);
p.then(() => console.log("done"));async return typing
async functions always return a Promise<U>. The body returns U, which TS wraps automatically.
async function fetchNumber(): Promise<number> {
await delay(10);
return 42; // inferred as Promise<number>
}
fetchNumber().then(n => console.log(n.toFixed(0)));await typing
await unwraps the Promise and yields the inner type (string here), so you get correct IntelliSense.
async function readUserName(id: number): Promise<string> {
await delay(5);
return `user-${id}`;
}
async function main(): Promise<void> {
const name = await readUserName(7); // name: string
console.log(name.toUpperCase());
}
main();Promise.all typing
Promise.all preserves tuple types; each element is awaited to its inner type for you.
async function both() {
const [n, s] = await Promise.all([
fetchNumber(),
readUserName(1),
]);
console.log(n, s);
}
both();Error typing
In catch, prefer unknown. Narrow with instanceof Error (or custom checks) before accessing fields.
async function safe() {
try {
await delay(1);
throw new Error("boom");
} catch (e: unknown) {
if (e instanceof Error) {
console.error("Handled:", e.message);
} else {
console.error("Unknown error", e);
}
}
}
safe();Async return type check
Quick check: What is the return type of an async function that returns a value of type U?
Recap
Recap: Promise<T> wraps eventual values, async functions return Promise<U>, await unwraps types, and errors in catch should start as unknown.
Frequently asked questions
Is the “Promise<T>, async/await typing” lesson free?
Yes — the full text of “Promise<T>, async/await typing” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. 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.
What will I learn in “Promise<T>, async/await typing”?
Understand Promise shapes, annotate async functions, and work with awaited values safely. 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; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Promise<T>, async/await typing” 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
- Promise , async/await typing
- Error typing (unknown), narrowing in catch
- Concurrency patterns (all/settled/race) types