Skip to main content
Module 8 · Lesson 1 of 315/22 in the course~16 min
Module lessons (1/3)

Promise<T> and Async / Await Typed

Lesson preview5 hands-on exercises readyCourse TypeScript · Module 8 — TypeScript Asynchronous & Promise

Understand the inference of asynchronous returns, Awaited<T>, and error flow in Promises.

What you'll learn in this lesson

  • Understand that every async function always returns a Promise<T>, even when you write return valore.
  • Use the built-in utilities Awaited<T> and ReturnType<typeof fn> to derive resolved types of asynchronous functions.
  • Avoid the classic bug of always truthy conditions on Promise not preceded by await.
  • Handle exceptions in async / await streams with try / 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

  1. 1How TypeScript types async and await
  2. 2The Promise non-waiting bug and asynchronous error handlers
  3. 3Guided practice
  4. 4Check for understanding
Pro Lesson · Module 8 — TypeScript Asynchronous & Promise

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.