Lesson 7 of 8
Aborting stale requests
- 15 min
- 4 checks
- 3 hints
- Go to task
Goal
Only the latest search's results show up
Explanation
In a live search, every letter you type sends a new request, and nothing guarantees the responses come back in the same order. The slow response for "p" can arrive after the response for "pen" and overwrite it, so the user sees results for a word they're no longer searching for.
AbortController cancels the request itself, not just your waiting for it. You pass its signal to fetch, and when you call abort(), the request rejects with an error named AbortError. So abort the previous request before every new one.
Aborting is a decision you made, not a failure, so don't show it to the user as an error: ignore only AbortError and let every other error show. Also note that a controller can only be used once, so create a new one for every request.
Your task
Every time the user types in #q, abort the previous request, pass the new request's signal to api, and ignore AbortError, so that only the response to the latest search appears in #out.
Checks
4 checks- A single search comes through as usual
- An old response doesn't overwrite a newer one
- The previous request is really aborted, not just ignored
- An abort doesn't show up to the user as an error