Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced20 minLESSON 25 / 34

async & await

01

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.

Choose what waits for what
PatternWhat waits?Good fit
await one operationOnly the enclosing async procedure pausesA later step needs this result
await in for...ofEach iteration waits before the nextOrder matters or concurrency should be limited
Promise.all with mapped promisesThe caller waits for all required resultsIndependent operations can overlap
forEach(async ...)forEach does not wait for returned promisesAvoid when completion or error collection matters
try/catch around awaitCatches the awaited rejectionRecovery 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.

02

Mental model

FOLLOW THE VALUE

readTotal()
async call
promise

The call returns a promise to its caller.

01 / 03Trace the transformation

03

Live example

There are two awaits with two callers. Each waits for the result it needs; showTotal owns the visible failure report.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Read the fulfilled result

Wait for readTotal before logging the total. Keep the catch around the awaited operation.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

7

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does every async function call return?
    02Does await make a long synchronous loop non-blocking?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson