Yo everyone, I’m wiring up a search box + list UI and I keep hitting a nasty failure mode where slower network responses overwrite newer results, so the UI “rewinds” and my loading spinner gets stuck.
let requestId = 0;
async function runSearch(q) {
const id = ++requestId;
setState({ loading: true, q });
const res = await fetch(`/api/search?q=${encodeURIComponent(q)}`);
const data = await res.json();
setState(s => ({ ...s, loading: false, items: data.items }));
}
What’s the cleanest pattern to guarantee only the latest request updates state (AbortController, id checks, queueing), without making cache/pagination edge cases worse?
WaffleFries