ROHINI, DELHI · LIVE ONLINE & CLASSROOM

Frontend asynchronous state exercise: stale responses

You type a new search while the previous request is still waiting. The newer result arrives first; then the older result replaces it. Each response can be valid on its own while the screen shows an answer to the wrong question. This worksheet makes that ordering problem visible with invented delays.

Use a disposable local practice project and fictional labels. No real endpoint, account or personal search history is needed. The small example below demonstrates result selection in JavaScript; it is not a complete browser interface or a production networking library.

Predict two different orders

Write down the order in which requests start, then the order in which their responses finish. Call the first request “older search” and the second “newer search”. Give the first a longer delay. A naive implementation that assigns every arriving result to the same display would finish with the older label, although the user most recently asked the newer question.

Keep “most recently requested” separate from “most recently received”. For this exercise the display should represent the most recent request. Another product may intentionally combine results or preserve a historical view; its acceptance rule must be stated before judging the implementation.

Try a request identity guard

let latest = 0;
let shown = "";
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
async function load(label, delay) {
  const request = ++latest;
  await wait(delay); // invented response delay
  if (request !== latest) return;
  shown = label;
}
await Promise.all([load("older search", 30), load("newer search", 5)]);
console.log(shown); // newer search

Save this example as stale-response.mjs and run it with a local Node.js runtime that supports top-level await. The expected printed label is newer search. The second call increments the identity before either delay finishes. When the older call resumes, its captured identity no longer matches and it cannot update shown.

The guard does not make the older operation disappear. It decides whether that operation may commit its result to this one display state. The exercise deliberately uses timers instead of a network. Do not describe a successful local run as proof that a real API was cancelled or that every timing case is handled.

Copy a boundary test sheet

Local scenarioExpected display rule
Older response finishes lastKeep the newest requested label.
Older response finishes firstIt cannot commit once a newer request has started.
Three requests finish out of orderOnly the latest identity may update the display.
Latest request failsShow its chosen error state; do not silently substitute an older success.
Screen is reset or removedInvalidate outstanding display work according to the interface design.

The last two rows are extensions to implement and test; the short sample has neither an error handler nor a screen lifecycle. Record that limit. A table describes expected behaviour, while an observed test result needs the actual code and output that exercised it.

Guard loading and error state too

In a fuller interface, inspect every state write. An older request's cleanup must not turn off the loading indicator for a newer request still in flight. Likewise, an old error should not overwrite the current result. Apply the same ownership question to loading, errors and displayed data rather than guarding only the happy path.

Choose what the screen shows while the current request waits: retained previous data, a blank area or an explicit loading state. Label retained data if the design needs that distinction. This worksheet does not prescribe one interaction for every search, form or dashboard.

Separate cancellation from display ownership

Browser APIs can support cancellation through an AbortController. MDN's abort reference describes aborting supported asynchronous operations, including fetch. Cancellation can be useful, but this sample does not call it. Check the API and error behaviour actually used by your application; do not assume cancelling a client wait reverses an action already accepted by a server.

Keep the display identity rule explicit even when adding cancellation. A response could already have completed, or an operation might not support the cancellation mechanism you chose. Explain which responsibility each mechanism handles and preserve failed observations instead of adding an unexplained delay until a demo looks right.

Record what another learner can repeat

Save the runtime version, request-start sequence, invented delays, expected final label and actual output. Try the reversed delays and a three-request sequence. Note separately which lifecycle or error cases remain unimplemented. You need a reproducible ordering exercise, not a claim of production race safety.

Compare the API pagination worksheet, which checks collection completeness, and ask about practical frontend learning. No particular course assignment or employment outcome is promised.

Admissions

Request a call back

Tell us which course you are interested in and the counselling desk will call you about fees, batches and a free demo class.

18008913982 (toll free) · +91 9899-355-335
[email protected]

Message us on WhatsApp

Open the enquiry form in a separate window ↗

Your enquiry goes to Coding Bytes through ViaCRM so the counselling team can respond. Please share only the details needed for your question, not passwords, payment details or sensitive documents. Privacy & enquiries · Before enrolling

Call WhatsApp Enquire