fetch & network errors
Understand the idea
A missing resource returns 404, yet fetch enters your success path. HTTP failure statuses do not reject fetch by themselves.
Several checkpoints, several possible failures
A request can reach a server successfully and still return an error response. It can return a successful status with malformed or unexpected data. Separating these cases lets you show a useful message instead of a vague “something went wrong” for every failure.
Getting a response is only the first check
Fetch returns a for a Response . That object carries status information and provides to read the body. It is not the final application data, so handing it directly to a view that expects a count skips several necessary checks.
HTTP is the request/response protocol used by the web. A response includes a status number and a body. fetch normally fulfills with a Response even for statuses such as 404 or 500; it rejects for failures such as a network problem or a browser-blocked request. Check response.ok before treating the body as successful application data.
| Status or failure | What it means | Typical next step |
|---|---|---|
| 200–299 (response.ok) | HTTP reports success | Read and validate the expected body; 204 may have no body |
| 400 | The server rejected the request input | Inspect the request and explain the input problem |
| 401 / 403 | Authentication missing or access forbidden | Handle identity or permission; do not retry blindly |
| 404 | The requested resource was not found | Check the URL or show an appropriate empty/error state |
| 429 | Too many requests | Respect retry guidance and reduce request frequency |
| 500–599 | A server-side failure | Report it and use bounded retries only when appropriate |
| Network or CORS rejection | No readable successful fetch response | Check connectivity or server access policy |
Read the status deliberately
response.ok reports a status in the 200–299 range. Decide how to handle other statuses at the point where you know what the request was trying to do. An empty account, a missing resource, and a server outage can require different messages, even though none gives you the requested items.
Parse and inspect the body
Use try/catch around the awaited workflow at a boundary that can report a useful error. Keep the stage that failed in the message: a malformed body is a different problem from a count field containing text. Avoid turning every failure into empty data, because that hides the difference between an outage and a genuinely empty account.
Reading response.json() is another step and can fail on invalid JSON. Successful parsing still does not validate your fields. The example checks count's type; an application might also require it to be finite, non-negative, and an integer. The data URL keeps this exercise independent of a live service, while the same checkpoints apply to an HTTP response.
Handle cancellation and stale results
Cross-origin permission is another checkpoint, controlled by the server’s response headers and the browser. Switching to no-cors does not produce readable JSON. Request access from the owner or route an authorized request through your own backend when that is an appropriate part of the application.
Use an AbortController when leaving the screen or replacing a request makes the result obsolete. Cancellation has to be connected through the request's signal. Even with cancellation, decide which request is allowed to update the current screen, so a slow older response cannot overwrite newer results.
Mental model
FOLLOW THE VALUE
A fulfilled request may still have an unsuccessful HTTP status.
01 / 03Trace the transformation
Live example
The data URL makes the success path deterministic. To exercise failure, replace count’s number with a string, or replace the URL with data:application/,invalid; the catch reports the validation or parsing failure.
Your console output appears here.
Guided challenge
Wait for the body
Await JSON parsing before validating and reading count.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
3
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.