Async functions return Promise-wrapped types. Use `Promise<T>` or `Awaited<T>` to model resolved values. Always consider error types when catching unknown. **Why it matters:** • Typed promises prevent you from forgetting awaited shapes • Catching unknown avoids assuming Error and crashing on non-Error throws • Adding AbortSignal avoids wasted work and makes UIs responsive
async function fetchUser(id: string): Promise<User> { ... }type User = { id: string; name: string };
async function fetchUser(id: string): Promise<User> {
const res = await fetch(`/users/${id}`);
if (!res.ok) throw new Error("Request failed");
return res.json() as Promise<User>;
}
// Awaited extracts inner type
type UserPromise = ReturnType<typeof fetchUser>; // Promise<User>
type UserResolved = Awaited<UserPromise>; // User
async function safeGet(id: string) {
try {
return await fetchUser(id);
} catch (err: unknown) {
if (err instanceof Error) console.error(err.message);
}
}
Understanding Async/Await is fundamental to mastering TypeScript. Practice with the examples above and experiment with variations to solidify your knowledge.