advanced · 25 minute study
Only the current request may commit
A slow old search can overwrite a newer result; account changes make the same bug a privacy boundary problem.
Useful preparation · 1 prerequisite
Use this as a map, not a lock. If these ideas are familiar, continue.
- A promise is an outcome, not a background thread — Correctly composing async work avoids accidental serial delays, unhandled failures and duplicated side effects.
First, build a mental model
Cancellation asks work to stop. Identity checks decide whether a result is still allowed to update state. Use both when the host may finish work before cancellation is observed.
- Increment a request version or capture a unique request identity.
- Abort obsolete fetch work where supported.
- Before committing, compare both request identity and relevant scope.
- Handle AbortError as cancellation, not as successful data or a generic failure alarm.
let latest = 0;
let controller;
async function search(query, commit) {
const version = ++latest;
controller?.abort();
controller = new AbortController();
const response = await fetch('/search?q=' + encodeURIComponent(query), { signal: controller.signal });
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();
if (version === latest) commit(data);
}- Request A
A captures version 1.
- Request B
B increments latest to 2 and asks A to abort.
- Late A
If A still resolves, version 1 cannot commit over version 2.
Production thinking
Take this beyond the example.
Keep cancellation, response validation, current-scope checks and server authorization explicit. Unit-test out-of-order completions and account switches.
Sources and further reading
Your evidence
unseen is a local learning signal, not a credential or proof of production competence.
Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.
Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.
Next lesson · Give every resource an owner →