Filtering, cancellation, and caching
Prevent stale requests from overwriting new results and avoid duplicate fetches.
- Difficulty
- beginner
- Time
- 50 minutes
- Last verified
- 9/21/2026
What you will build
Prevent stale requests from overwriting new results and avoid duplicate fetches.
Understand the idea first
Prevent stale requests from overwriting new results and avoid duplicate fetches.
Build it step by step
let controller;
async function search(query) {
controller?.abort();
controller = new AbortController();
return loadData(`/api/search?q=${encodeURIComponent(query)}`, {
signal: controller.signal,
});
}- Open the previous project and record its result.
- Type the example and complete: Search three terms quickly; only the final query may update the UI.
- Change one input, predict, then verify.
- Create and repair: Cancellation is not a user-facing failure; handle AbortError separately.
Read the important lines
- Prevent stale requests from overwriting new results and avoid duplicate fetches.
- Read input, processing, and output separately.
- Verify another input and an edge case.
Common mistakes and what to check
| Symptom | What to check |
|---|---|
| Unexpected result | Cancellation is not a user-facing failure; handle AbortError separately. |
| Nothing changes | Save, verify paths, and read the first relevant Console error. |
Practice without copying
Search three terms quickly; only the final query may update the UI.
Check the answer after you try
Rebuild and explain the decisions. Check: Cancellation is not a user-facing failure; handle AbortError separately.
Completion check
- I produced the required visible result
- I can explain the input, processing, and output
- I tested normal, edge, and failure cases