LLearnCode
Academy/javascript/Browser and asynchronous engineering

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.

Loading progressEvidence grows with practice
Useful preparation · 1 prerequisite

Use this as a map, not a lock. If these ideas are familiar, continue.

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.

  1. Increment a request version or capture a unique request identity.
  2. Abort obsolete fetch work where supported.
  3. Before committing, compare both request identity and relevant scope.
  4. Handle AbortError as cancellation, not as successful data or a generic failure alarm.
Read the codejavascript · educational example
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);
}
  1. Request A

    A captures version 1.

  2. Request B

    B increments latest to 2 and asks A to abort.

  3. 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 →