Skip to content

QuizNot attempted

Stale search responses

Search-as-you-type: the user types "re" then "react". The "re" response arrives AFTER the "react" one and overwrites the results. Best fix?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • Abort the previous request with AbortController (or ignore responses that are not the latest request id)
  • The bug is out-of-order responses, so you must cancel or ignore stale requests: abort the previous fetch with AbortController, or tag each request with an id and drop responses that aren't the latest.
  • Not a longer debounce: fewer requests, but two in flight can still return out of order.
  • Not throttle: sends even more requests during typing.
  • Not Promise.race: races promises you pass it, it can't know which response is stale.
  • React Query and SWR handle this for you.