Lesson 7 of 8

Aborting stale requests

Goal

Only the latest search's results show up

01

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.

02

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
  1. A single search comes through as usual
  2. An old response doesn't overwrite a newer one
  3. The previous request is really aborted, not just ignored
  4. An abort doesn't show up to the user as an error
03

Hints

0/3
Track lessons0 of 8 done

Learn to read JSON, wait for the server to respond, and handle loading and failure states. That's what separates a demo from a product people can rely on.

Intermediate level, 8 lessons, 2 hours in total.

index.html
Preview
index.html100%
–/4

Press “Run and check” to grade your code.

4 checks listed under “Your task”.

Track lessons0 of 8 done

Learn to read JSON, wait for the server to respond, and handle loading and failure states. That's what separates a demo from a product people can rely on.

Intermediate level, 8 lessons, 2 hours in total.