Asynchronous Programming13% of the exam
Promises, async and await
How a promise settles once, how values and errors travel through then, catch and finally, which of Promise.all, allSettled, race and any fits a requirement, how to wrap a callback API in a promise, and how async functions and await behave, including sequential versus parallel awaits, missing await or return bugs, and top-level await.
Given a scenario, apply asynchronous programming concepts.
~7 min read · 21 practice questions ·
What the exam expects
- Predict what a promise chain logs: settle-once, values returned (or not) from
then, errors skipping tocatch, recovery aftercatch, and the transparentfinally. - Pick
Promise.all,allSettled,raceoranyfrom a requirement, and predict result order and failure behavior. - Wrap a callback API in
new Promiseand flatten nested callbacks withasync/await. - Use
asyncfunctions correctly: they always return a promise,awaitruns steps sequentially unless you start them first, and a missingawaitorreturnloses ordering and errors. - Key terms: pending / fulfilled / rejected (a promise's states; settled means not pending); executor (the function passed to
new Promisethat callsresolveorreject); chain (eachthenreturns a new promise); floating promise (one started but neither returned nor awaited); top-level await (awaitoutside any function, valid only in modules).
Key facts
Settle once
- Detail
- Only the first
resolve/rejectcall counts
then callback return- Detail
- Becomes the next promise's value; none means
undefined
finally callback- Detail
- No argument; return value ignored unless it throws or rejects
Promise.all- Detail
- Input-order array; rejects on first rejection
Promise.allSettled- Detail
- Never rejects;
{status, value/reason}per input
Promise.race- Detail
- Mirrors the first input to settle
Empty input array
- Detail
allfulfills with[];anyrejects withAggregateError;racestays pending forever
Promise.any all rejected- Detail
AggregateError;errorsin input order
Async function result
- Detail
- Always a promise; uncaught throw rejects it
Top-level
await- Detail
- Only in modules; elsewhere a
SyntaxError
forEach with async callback- Detail
- Does not wait for the promises
Response body
- Detail
- Readable once;
clone()before reading to reuse
Sources for this section (11)
- Promise() constructordeveloper.mozilla.org (opens in a new tab)
- then()developer.mozilla.org (opens in a new tab)
- finally()developer.mozilla.org (opens in a new tab)
- Promise.all()developer.mozilla.org (opens in a new tab)
- Promise.allSettled()developer.mozilla.org (opens in a new tab)
- Promise.race()developer.mozilla.org (opens in a new tab)
- Promise.any()developer.mozilla.org (opens in a new tab)
- async functiondeveloper.mozilla.org (opens in a new tab)
- awaitdeveloper.mozilla.org (opens in a new tab)
- forEach()developer.mozilla.org (opens in a new tab)
- Using the Fetch APIdeveloper.mozilla.org (opens in a new tab)
- Why does `100 - getDiscount()` log `NaN` when `getDiscount` is an async function returning 10?
- When should you choose `Promise.race` over `Promise.any`?
- Why does a chain continue normally after a `catch` that returns a value?
- In what order does `Promise.all` return results from inputs that finish at different times?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the JavaScript Developer pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 24 lessons of the JavaScript Developer guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (21 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams