Abuzar Ashfaq
All experiments

Request Weather.

AN ARRIVAL-ORDER STUDY

The last response isn’t always the right one.

Run two simulated searches: Oslo, then Lisbon. Change the delays and watch which result stays.

Simulated requests · fictional weather

Same requests. Two outcomes.

  1. 0 ms: Oslo request A starts.
  2. 400 ms: Lisbon request B starts. Lisbon is now the selected location.
  3. 1,100 ms: B returns. Both interfaces show Lisbon, 22°C and Clear.
  4. 2,400 ms: A returns. Without a guard, Oslo, 8°C and Cloudy replaces Lisbon. A latest-request guard ignores A and keeps Lisbon.

Interactive playback requires JavaScript.

Why this happens

Requests finish in arrival order. Your intent follows selection order. Both interfaces receive identical responses, but one applies every completion. The other checks whether the request is still current before updating the result.

// Simplified example: each search gets a new ID.
const requestId = ++latestRequestId;
const result = await loadResult(query);
if (requestId !== latestRequestId) return;
showResult(result);

This local simulation sends no network requests. Ignoring an old result does not cancel its work. Production code also needs to handle current errors, disposal and cancellation where supported.

Events at the same simulated time process selections first, then responses A before B. This is a convention for a reproducible lesson; real networks do not guarantee that order.

Read React’s explanation of request races ↗

Explore all work · Email me