AbortController
Cancel an in-flight fetch when the user starts a new search.
Pass signal from an AbortController into
fetch. Calling abort() rejects that fetch
(often with AbortError). Abort the previous controller
before starting a new request so stale responses do not overwrite the UI.
Results appear here.
Important JavaScript
let controller = null;
async function load(id) {
if (controller) {
controller.abort();
}
controller = new AbortController();
const response = await fetch(url, { signal: controller.signal });
// …
}