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.
0 of 10 checks passed
Your progress is saved on this device.