I type a search, change it and expect results for the query I left in the field. That seems straightforward until requests finish in a different order from the one in which they started.
The first request can take longer and overwrite the second. The screen then shows valid information for an intention I have already abandoned.
For a search or remote filter, I want to address that situation before considering the interaction complete.
Finishing does not mean staying current
Await orders steps within one execution. It does not stop another execution of the same function from advancing while the first waits.
I need to decide which request still has permission to update the interface. For a search where only the current query matters, I can assign a version to each attempt.
That is not a universal rule. Accumulating paginated results or performing writes requires a different contract; blindly discarding older results could lose necessary information.
Cancel, then check before displaying
This example creates a small controlled search. It assumes a custom /api/search endpoint returning an array of strings; that API must be implemented. render receives states and must display text safely, for example through textContent.
function createSearch(render) {
let version = 0;
let pending;
let active = true;
async function load(query) {
if (!active) return;
const mine = ++version;
pending?.abort();
const controller = new AbortController();
pending = controller;
render({ state: 'loading' });
try {
const response = await fetch(
'/api/search?q=' + encodeURIComponent(query),
{ signal: controller.signal }
);
if (!response.ok) throw new Error('HTTP ' + response.status);
const items = await response.json();
if (!Array.isArray(items) || items.some(x => typeof x !== 'string')) {
throw new TypeError('Expected an array of strings');
}
if (!active || mine !== version) return;
render({ state: items.length ? 'ready' : 'empty', items });
} catch {
if (!active || mine !== version || controller.signal.aborted) return;
render({ state: 'error' });
}
}
return {
load,
dispose() { active = false; version += 1; pending?.abort(); }
};
}
Cancellation attempts to stop unnecessary work. The version protects updates if an earlier operation still completes its steps. On unmount, dispose also removes permission to publish results.
Every exit is not an error
Canceling because someone typed a different query does not require an error alert. Visible state should belong to the current query.
A failure in that current request does need a clear outcome. The example separates loading, results, empty results and error. An empty array does not mean the server failed.
The MDN fetch guide explains another important detail: an HTTP error does not itself reject the promise. I therefore check response.ok before interpreting the body.
Debounce answers another question
Waiting for a typing pause can reduce how many queries start. It does not guarantee the arrival order of requests already running.
Adding debounce is a separate decision. Its timer needs cleanup, and clearing the field needs an explicit policy. This example focuses on request relevance rather than providing a complete search interface.
Test a less convenient network
I want to force the awkward order: start A, start B, resolve B and finally resolve A. B must remain visible. I would also check HTTP failure, an empty response and unmounting while a request is pending.
AI can help construct those controlled responses. My criterion is what should remain on screen, not a simulation that always finishes in the easiest order.
Receiving data is only part of an asynchronous interface. I also need to know whether that data still belongs on screen.
AN IDEA TO SHARE



