Intermediatenote 01 of 0803 reasons · 03 notes
JavaScriptIntermediate
Abort stale fetch requests
Cancel outdated browser requests so fast UI changes do not render old data.
Code example
Copy, adapt, and ship.
Read the numbered lines, copy the complete snippet, then adjust the values for your project.
let activeController; async function loadResults(query) { activeController?.abort(); activeController = new AbortController(); const response = await fetch(`/api/search?q=${encodeURIComponent(query)}`, { signal: activeController.signal, }); return response.json();}Why this matters
The production reason.
- 01
Search boxes, filters, and tabs can fire several requests quickly. Without cancellation, an older response can arrive last and overwrite newer UI.
- 02
AbortController lets you tell the browser that the previous request no longer matters.
- 03
This keeps fast interfaces honest: the rendered result matches the latest user intent, not whichever network request finished last.
How it holds up outside a demo.
- JavaScriptUse AbortController with fetch for cancellable requests.
- ReactCreate a controller inside useEffect and abort it in the cleanup.
- Next.jsUse this in client components for live search, filters, and dashboards.
Explore other stacks
Jump into a different technology.
08 JavaScript notes on this shelf, and six more stacks beside it.
Use next/image for image optimization
Next.js image optimization starts with next/image sizing, lazy loading, modern formats, and predictable dimensions.
Open the noteKeep state close to the interaction
Local state avoids unnecessary parent renders and keeps component intent obvious.
Open the noteBuild a CSS gradient generator with code export
A CSS gradient generator should preview color stops, preserve accessible contrast, and export clean CSS code.
Open the noteFix WordPress H1 SEO issues
Fix WordPress SEO issues by keeping one clear H1, escaping template output, and compressing images before WordPress upload.
Open the noteReduce LCP with image optimization
Reduce LCP with image optimization by resizing, converting, and compressing images before they reach production.
Open the noteUse real buttons for actions
If it changes state, use button. If it navigates, use a link.
Open the note