async & await
Understand the idea
A is marked , but the caller still prints a Promise instead of the data. Async changes the function’s return contract.
Pause one procedure, not the whole page
When one result is needed for the next calculation, a sequence is easier to follow than deeply nested s. await expresses that dependency directly. It still uses promises underneath, so promise outcomes and failure ownership continue to matter.
What async returns
An async function always returns a promise. Returning an ordinary value fulfills it, and throwing rejects it. Await unwraps a fulfilled result for the next or throws the rejection reason at the await , where an enclosing catch can handle it.
An async function always returns a promise. Returning 7 makes that promise fulfill with 7; throwing makes it reject. Calling the function without await gives you the promise, not the eventual number. That is why trying to add directly to an async function call does not perform the calculation you intended.
| Pattern | What waits? | Good fit |
|---|---|---|
| await one operation | Only the enclosing async procedure pauses | A later step needs this result |
| await in for...of | Each iteration waits before the next | Order matters or concurrency should be limited |
| Promise.all with mapped promises | The caller waits for all required results | Independent operations can overlap |
| forEach(async ...) | forEach does not wait for returned promises | Avoid when completion or error collection matters |
| try/catch around await | Catches the awaited rejection | Recovery at this level of the operation |
Where await resumes
Await does not block the whole JavaScript thread while a pending operation completes. However, work before or between awaits still blocks normally. Making a CPU-heavy function async does not move that work onto a worker.
Catch the work you actually await
Use a try/catch at the boundary that can explain or recover from the failure. You do not need a separate catch around every await if a caller intentionally handles the rejection. You do need an owner for each failure; a fire-and-forget call without handling can become an unhandled rejection.
An awaited rejection behaves like a throw at the await point, so a surrounding catch can handle it. A promise you start and do not return or await is outside that connection. At the top of an interaction, give that outstanding operation an explicit catch if nothing else will handle it.
Sequential or independent work?
Sequential awaits are correct when the second step needs the first result. For independent operations, starting them together and awaiting Promise.all can avoid unnecessary waiting. forEach does not await returned callback promises; use a for...of loop for deliberate sequential work or map to promises and await their collection for concurrent work.
For dependent steps, awaiting one before starting the next is correct. For independent requests, start them together and await Promise.all if every result is required. An async callback passed to forEach does not make forEach wait. An awaited for...of loop gives you sequential behavior; mapping to promises plus Promise.all gives you concurrent waiting.
Mental model
FOLLOW THE VALUE
The call returns a promise to its caller.
01 / 03Trace the transformation
Live example
There are two awaits with two callers. Each waits for the result it needs; showTotal owns the visible failure report.
Your console output appears here.
Guided challenge
Read the fulfilled result
Wait for readTotal before logging the total. Keep the catch around the awaited operation.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
7
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.