Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Asynchronous JavaScript  ›  Capstone Project

async/await

async/await 14 minasync/await is syntax sugar over Promises: an async function always returns a Promise, and await pauses that function (not the whole program) until the awaited Promise settles, letting asynchronous code read top to bottom like synchronous code, with ordinary try/catch for errors.

async/await

In short: async/await is sugar over Promises, not a different mechanism. An async function always returns a Promise. Inside it, await somePromise pauses that function — not the whole program — until the Promise settles, then hands back the resolved value, letting asynchronous code read top to bottom like ordinary synchronous code. A rejected awaited Promise becomes a thrown error, catchable with a completely normal try/catch.

The same event-loop kitchen: async function checkMug() with await checkStock("mug") reads top to bottom like sync code; one panel shows await pauses THIS function while other code still runs, another shows try/catch around await turning a rejected Promise into a normal thrown error; a note that awaiting one thing at a time is sequential, Promise.all still runs them together

The same Promise, read top to bottom

Compare this with the .then() chain from the last lesson — same operation, much flatter:

javascript

checkMug is an async function, so it returns a Promise automatically. Inside it, await checkStock("mug") pauses right there until that Promise resolves, then result holds the actual string — no .then() callback needed. It reads exactly like synchronous code, even though it isn't.

Catching a rejection with ordinary try/catch

await turns a rejected Promise into something catch already knows how to handle:

javascript

When checkStockStrict("sold-out-item") rejects, await re-throws that rejection as a regular JavaScript error at the point of the await — the surrounding try/catch catches it exactly the way it would catch a throw. No .catch() method call needed; it's the same error handling you already know.

Sequential awaits vs awaiting together

The trap: awaiting one thing at a time, when the steps don't actually depend on each other, makes independent work wait in line for no reason.

javascript

sequential doesn't start checking "notebook" until "mug" has fully settled — two waits, back to back. concurrent starts both checks immediately with Promise.all, so the total time is roughly one check's worth, not two. Use sequential awaits only when a later step genuinely needs an earlier result; otherwise, start independent work together.

Quick practice

quick drill

What does an async function always return, even if it has no explicit return statement?

quick drill

What does await somePromise do when that Promise is already rejected?

quick drill

Why might await checkStock("mug"); await checkStock("notebook"); be slower overall than await Promise.all([checkStock("mug"), checkStock("notebook")]);?

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainSequential vs concurrent awaits

Explain the difference in total waiting time between await checkStock("mug"); await checkStock("notebook"); (two separate await lines) and await Promise.all([checkStock("mug"), checkStock("notebook")]);, and why someone might pick one over the other.