Async and Promises
Async work finishes later.
Think network calls, timers, or file reads.
A Promise is a box that will hold a value later.
TypeScript types Promises so you know what value resolves.
Promise<T>
A Promise<string> resolves to a string.
Resolve means the box finally opens with a value.
Write the type or let guessing pick T from the return value.
async / await
async functions always return a Promise.
await unwraps the resolved value.
Unwrap means take the toy out of the box.
TypeScript types that value for the rest of the block.
Typing settled results
Promise.all waits for every promise.
It keeps a tuple of result types.
Promise.allSettled gives you status tagged outcomes.
That helps when some tasks may fail.
Errors in async code
Rejected promises throw at the await.
Rejected means the box failed to open with a value.
Wrap risky awaits in try/catch.
Under strict settings, the error in catch is unknown.
Narrow before use.
Typing async helpers
Always type what a Promise resolves to.
Then async/await stays as clear as sync code.
Sync means step by step right now.
Always type what a Promise resolves to. Then async/await stays as clear as sync code.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.