Axios advantages over fetch
knowledge of HTTP client ergonomics.
Axios auto-parses JSON, rejects on HTTP error status, and offers interceptors, timeouts, and cancellation out of the box.
claiming fetch rejects on 404 or 500.
What's really being asked
Whether you understand fetch's default behavior and can articulate concrete ergonomic and reliability gains from Axios, rather than preferring it by habit.
The full answer
fetch resolves the promise for any HTTP response, including 404 and 500, and rejects only on network-level failures, so you must manually inspect response.ok and explicitly parse with await response.json(). Axios automatically serializes request bodies and parses JSON responses into response.data, and it rejects the promise for non-2xx statuses, so a single catch handles HTTP errors. Axios also provides interceptors to attach auth tokens or log centrally, a built-in timeout option, request cancellation, automatic retries via plugins, and createable instances with a baseURL and default headers. These reduce repetitive boilerplate across a React Native codebase.
The mistakes people make
Saying fetch rejects on 4xx or 5xx, claiming fetch cannot set headers or send JSON, or asserting Axios is mandatory. Overlooking that fetch can now cancel via AbortController, narrowing one historical Axios advantage, also shows shallow knowledge.
What usually comes next
How do you replicate Axios interceptors with a fetch wrapper? When is fetch preferable to avoid an extra dependency? How does Axios handle timeouts compared with AbortController?
A concrete example
With fetch you write const res = await fetch(url); if (!res.ok) throw new Error(res.status); const data = await res.json();. With Axios the same call is const { data } = await axios.get(url);, and a 500 automatically lands in catch. An interceptor configured once injects the bearer token into every request, eliminating the need to set the Authorization header on each call manually.
Interview question
A developer expects fetch to throw when the server returns HTTP 500. Why does their catch block never run?
- a.The 500 response body is empty, so await response.json() silently returns null
- b.fetch retries 500 responses automatically until one succeeds, hiding the error
- c.fetch routes server errors to a separate onerror callback instead of the promise
- d.fetch only rejects on network failures; HTTP error statuses still resolve successfullyCorrect
Why? this is the answer
fetch resolves its promise for any received HTTP response and rejects only on network errors, so a 500 does not throw. Axios differs by rejecting on non-2xx. fetch has no auto-retry or separate onerror channel, so those are wrong.
Just read this? Test yourself on what you have been reading.
Read the original → codercrafter.in
- #axios
- #fetch
- #networking
- #http-client
- #interceptors
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles