Skip to content

Chapter 22 of 33

Async and Await

Write readable asynchronous workflows with concurrency, cancellation, and error boundaries.

42 minutes 10 quick checksBy Subha Prasad
Lesson 22 of 33Course navigation

Lesson content

Read, practise, then check your understanding

An async function always returns a Promise. await suspends that function, not the entire thread or event loop.

Preserve concurrency

async function dashboard() {
  const profileTask = fetchProfile();
  const activityTask = fetchActivity();
  try {
    const [profile, activity] = await Promise.all([profileTask, activityTask]);
    return { profile, activity };
  } catch (error) {
    throw new Error("Dashboard failed", { cause: error });
  }
}

Start independent operations before awaiting together; sequential awaits are correct only for dependencies. Use try/catch around the boundary that can recover or add context. Await async results or intentionally mark fire-and-forget work with error handling. for await...of consumes async iterables with backpressure-like sequential demand.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes async function?
Which JavaScript term matches this description: A function whose calls always return Promises.
Which statement best describes await?
Which JavaScript term matches this description: Suspension of an async function until a value is adopted and settled.
Which statement best describes sequential await?
Which JavaScript term matches this description: Starting one operation after the previous awaited operation completes.
Which statement best describes concurrent start?
Which JavaScript term matches this description: Starting independent Promises before awaiting their combined results.
Which statement best describes async iteration?
Which JavaScript term matches this description: Consumption of an AsyncIterable with for await...of.

0 of 10 checks passed

Your progress is saved on this device.

Async and Await | JavaScript Lesson | Subha Prasad