Module lessons (1/3)
Promise<T> and Async / Await Typed
Understand the inference of asynchronous returns, Awaited<T>, and error flow in Promises.
What you'll learn in this lesson
- Understand that every
asyncfunction always returns aPromise<T>, even when you writereturn valore. - Use the built-in utilities
Awaited<T>andReturnType<typeof fn>to derive resolved types of asynchronous functions. - Avoid the classic bug of always truthy conditions on
Promisenot preceded byawait. - Handle exceptions in
async / awaitstreams withtry / catch (err: unknown)and typed fallback returns.
Overview
JavaScript performs asynchronous operations via Promise. Without TypeScript it's very easy to forget await when faced with an asynchronous call, discovering too late that you're passing [object Promise] to a UI component. With strict: true, the compiler strictly distinguishes T from Promise<T>.
If you import a async function from a module without an exported interface for its return value, combine Awaited and ReturnType:
Sections covered in this lesson
- 1How TypeScript types
asyncandawait - 2The
Promisenon-waiting bug and asynchronous error handlers - 3Guided practice
- 4Check for understanding
Unlock "Promise<T> and Async / Await Typed" and the full Course TypeScript curriculum
Understand the inference of asynchronous returns, Awaited<T>, and error flow in Promises.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course TypeScript plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.