02 · Event Loop & Asynchronous JavaScript
harddebuggingjavascript~15 min

Fix Stale Search Responses

Objective: Prevent race conditions by ignoring responses that are no longer current.

Typing quickly fires several searches, and a slow early response overwrites a newer result. Fix `createSearch` so only the latest search can update the results.

  • Only the most recent search(query) call may call setResults.
  • Responses (and errors) from superseded searches are ignored.
  • The latest search still propagates its own errors.
JavaScript

⌘/Ctrl + Enter to run tests · Esc then Tab leaves the editor

Output
> Nothing has run yet.
Interview follow-up questions
  • How would AbortController improve this, and what does it not solve?
  • How do React Query or SWR handle this internally?
  • What about out-of-order writes to a server (not just reads)?