Race-Safe Search Feedback
搜索请求竞态反馈
A newer query cancels the slower old one and controls loading and result updates.
Where to use it
Keep remotely filtered lists trustworthy when network timing varies. Tie both loading and empty states to the latest query when remote results can arrive out of order.
Practical example
In a spare-parts lookup, someone types “wheel,” then quickly changes it to “wheel bearing.” Only the newer query controls the spinner and the visible part matches, even if the first request finishes later.
AI prompt
Make [typeahead] race-safe. Abort the previous request, guard every completion against a unique query revision, and show loading and empty feedback for the current query only.
Example code
Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.
View HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Race-Safe Search Feedback</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.ui { width: min(100%, 238px); display: grid; gap: 8px; position: relative; font-size: 12px; line-height: 1.35; }
.ui .surface { min-width: 0; border: 1px solid #676591; border-radius: 10px; background: #282744; padding: 8px; }
.ui .bar { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.ui .actions { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 5px; }
.ui .muted { color: #cecae9; font-size: 11px; }
.ui .center { text-align: center; }
.ui .mono { font: 11px ui-monospace, monospace; font-variant-numeric: tabular-nums; }
.ui p { margin: 0; }
.ui .button { min-height: 30px; padding: 4px 7px; border-radius: 7px; border: 1px solid #79779f; background: #3d3a60; color: #f9f5ff; font-size: 11px; font-weight: 650; cursor: pointer; }
.ui .button:hover { background: #4e4976; }
.ui .primary { background: #c5eeec; border-color: #dafaee; color: #17213b; }
.ui .primary:hover { background: #e3fff9; }
.ui .button:disabled { opacity: .6; cursor: default; }
.ui .field { min-width: 0; width: 100%; border: 1px solid #79779f; border-radius: 6px; background: #1b1a31; color: #faf5ff; padding: 5px 7px; font-size: 12px; }
.ui .field::placeholder { color: #c2bcdd; }
.ui input[type="range"] { accent-color: #b8ebe0; width: 100%; min-width: 0; margin: 0; }
.ui button { font: inherit; }
.ui [hidden] { display: none !important; }
.ui progress { accent-color: #b8ebe0; }
@media (prefers-reduced-motion: reduce) {
.ui *, .ui *::after, .ui *::before { transition-duration: 0s !important; animation-duration: 0s !important; scroll-behavior: auto !important; }
}
.lookup{min-height:68px;display:grid;align-content:start;gap:5px;border-inline-start:3px solid #968dc1}.lookup[aria-busy=true]{border-inline-start-color:#edc48d}.lookup[data-current=fast]{border-inline-start-color:#a7e6d5}
</style>
</head>
<body>
<main class="demo">
<div class="ui"><div class="actions"><button class="button" type="button" data-query="slow">Slow query</button><button class="button primary" type="button" data-query="fast">Fast query</button></div><div class="surface lookup" aria-busy="false"><div class="bar"><strong>Search results</strong><span class="mono" role="status">Idle</span></div><p class="muted result">Choose a query</p></div></div>
</main>
<script>
const lookup = document.querySelector('.lookup');
const status = lookup.querySelector('[role=status]');
const result = lookup.querySelector('.result');
const content = {"slow":"Sparse forest","fast":"Open coastline"};
let controller = null;
function run(query, signal) {
return new Promise((resolve, reject) => {
const abort = () => { clearTimeout(timer); reject(signal.reason); };
const timer = setTimeout(() => { signal.removeEventListener('abort', abort); resolve(content[query]); }, query === 'slow' ? 1600 : 420);
signal.addEventListener('abort', abort, { once: true });
});
}
async function search(query) {
controller?.abort(); const request = new AbortController(); controller = request;
lookup.setAttribute('aria-busy', 'true'); status.textContent = "Searching…";
result.textContent = query === 'slow' ? "Slow query queued" : "Fast query queued";
try { const answer = await run(query, request.signal); if (controller !== request) return; lookup.dataset.current = query; result.textContent = answer; status.textContent = "Found 1"; }
catch (error) { if (!request.signal.aborted && controller === request) status.textContent = "Could not load"; }
finally { if (controller === request) lookup.setAttribute('aria-busy', 'false'); }
}
for (const button of document.querySelectorAll('[data-query]')) button.addEventListener('click', () => search(button.dataset.query));
</script>
</body>
</html>Original example by UI Skills