Storage and fetching · mid
fetch in depth: errors, AbortController and timeouts
fetch only rejects on network failure, not on 404 or 500, so check response.ok; AbortController cancels a request (and is how you fix race conditions in search), and AbortSignal.timeout adds timeouts.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
if (!res.ok) throw new Error(res.status)— a 500 still resolves.- const c = new AbortController(); fetch(url, { signal: c.signal }); c.abort() rejects with an AbortError (name 'AbortError').
- AbortSignal.timeout(5000) and AbortSignal.any([a, b]) combine timeouts and user cancels.
- Race condition: typing 'ab' then 'abc' can show 'ab' results last; abort the previous request or ignore stale responses.
- The body is a stream: res.json() can only be read once (use res.clone()).
The red pen
Common traps: mistake → fix
Mistake: "fetch rejects on 404"
Fix: it resolves; only network errors and aborts reject.
Mistake: "aborting is only for unmount"
Fix: it is the main tool against out-of-order search results.
Easy to hard
Questions interviewers ask, easy to hard
- How do you handle HTTP errors with fetch?
- How do you cancel a request and why?
- How do you fix a search box showing stale results?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.