What's the diff between fetch and xhr type? Which one should I use? or Can someone explain the diff b

16 Replies, 1057 Views

Hey folks!

Quick question – can someone break down the *diff between fetch and xhr type* for me? I've used both but still kinda fuzzy on when to pick one over the other.

From what I get, fetch is newer, promise-based, and cleaner syntax, while XHR (XMLHttpRequest) is the old-school way with more boilerplate. But XHR has progress events, which fetch doesn’t natively support (without extra work).

Any pros/cons y’all wanna share? Like, is fetch always the better choice now, or are there cases where XHR still wins?

Also, anyone run into weird quirks with either? I’ve heard fetch can be tricky with error handling.

Thanks in advance! 🙌
Yo! Great question on the diff between fetch and xhr type.

Fetch is def more modern and easier to read, but XHR ain’t dead yet. Like you said, XHR has progress events built-in, which is huge for file uploads or big downloads.

Fetch’s error handling is weird tho—it only rejects on network fails, not HTTP errors (like 404s). You gotta check `response.ok` manually. Annoying, but fixable.

If you’re lazy like me, check out `axios`—it’s like fetch but with better defaults and error handling.
The diff between fetch and xhr type boils down to legacy vs. modernity.

XHR is clunky but battle-tested. Fetch is sleek but missing some features (like canceling requests easily).

For progress events, you can polyfill fetch with libraries like `fetch-progress` or just stick with XHR.

Honestly? If you’re starting fresh, go fetch. If you need fine-grained control, XHR still has its place.
fetch vs xhr? fetch is the cool kid, but xhr’s the old reliable.

big thing ppl miss: xhr works in older browsers outta the box. fetch needs polyfills if you care about IE or ancient mobile browsers.

also, xhr’s `abort()` is straightforward. fetch needs `AbortController`, which feels extra.

tools like `swr` or `react-query` abstract this stuff away tho—might be worth a look!
Kinda surprised no one’s mentioned streaming yet!

XHR can’t do streaming responses natively, but fetch can with `response.body`. That’s a game-changer for real-time stuff.

Downside? fetch’s error handling is *trash*. Miss a 500? Tough luck unless you write extra checks.

XHR’s `onerror` is simpler for sure.
OP here—wow, thanks for all the insights!

Tried wrapping fetch in a helper like @user5 suggested, and it’s way cleaner now. Still kinda miss XHR’s progress events tho.

Anyone got a favorite lib for adding progress to fetch? Or is it just easier to fall back to XHR for uploads?

Also, big +1 to the MDN docs shoutout. Lifesaver. 🙏
fetch’s syntax is *chef’s kiss*, but man, the quirks…

No cookies by default? Gotta set `credentials: 'include'`. Why??

XHR’s ugly but predictable. fetch feels like it’s hiding stuff from you.

If you’re debugging, `curl` or Postman can help test APIs before committing to either.
XHR fans rise up!

fetch is shiny, but XHR’s `upload.onprogress` is unbeatable for UX. Want a progress bar? XHR’s your buddy.

fetch can kinda do it with `ReadableStream`, but it’s a headache.

Also, XHR’s `timeout` property is simpler than fetch’s `AbortController` circus.
fetch’s promise-based flow is *so* much nicer than XHR’s callback hell.

But yeah, error handling’s a pain. Pro tip: wrap fetch in a helper function that checks `response.ok` and throws on bad statuses.

XHR’s still king for niche cases tho, like tracking upload progress.

Check out the Fetch API docs on MDN—super helpful for edge cases.



Users browsing this thread: 1 Guest(s)