0
Challenge · 49Routing & Data
Fetch API
Coreused in ~95% of apps
Almost every app you build has to ask a server for data. fetch is the built-in way to make that request — but getting it right means handling four states, checking for errors the browser won't throw for you, and cancelling stale requests so the wrong response never wins.
Core idea. fetch(url) returns a Promise; you await the response, check res.ok yourself, then await res.json() to read the body — all wrapped in try/catch, while you model the UI as idle → loading → success/error.
poke it, drag it,
try to break it
try to break it
no slides here.
you drive.
you drive.
Playground, this isn't a screenshot
current stateIDLE
idle→loading→success→error
what the user sees
No data yet
press Load feed to start a request
request log (top → bottom)
Press Load feed. Then press it again while loading to watch the first request abort.
idle → loading → success or error — every fetch moves through this machine. Re-trigger while loading and the older request is aborted so a stale response can never overwrite a newer one.
↑ play a little first, then dig in