DEV Community

Cover image for Axios or Fetch?- in 2 minutes

Axios or Fetch?- in 2 minutes

thatIITgirl on June 26, 2020

Ever wondered why developers are going for Axios over fetch? As we are aware both are the means to deal with HTTP or XMLHttp requests, Both are cap...
Collapse
 
sgoulas profile image
sgoulas • Edited

This post seems to miss the biggest differences. Axios automatically converts fetched data to a JSON where fetch requires the dev to do it. Also axios implements cancelable requests by exposing a cancel function where with fetch the dev has to override the API to cancel the request.

Also, with axios you get easy time out configuration and http interceptors on requests and responses.

Collapse
 
brogrammerben profile image
Ben

The interceptors are my major selling point.

Collapse
 
fasani profile image
Michael Fasani

Yea +1 for the interceptors!

Collapse
 
kosich profile image
Kostia Palchyk • Edited

Nice and quick overview, Ash! And just in time, thanks!

--

I wanted to learn more about fetch promise failing reasoning after your post. To share, heres a quote from MDN fetch:

The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500. Instead, it will resolve normally (with ok status set to false), and it will only reject on network failure or if anything prevented the request from completing.

So, "bad news" is still "news" in fetch implementation.

--

UPD: also, axios and fetch have different cancellation API.

Collapse
 
awesomeironman profile image
Cyrus Gracias

When we add another library it increases customers JS code download time
So we are basically making our site slow for customers using slower connections
And for what? Developer experience? That sounds bad to me

Collapse
 
aidurber profile image
Andrew Durber

It's 4.4KB min+gzip so it's pretty negligible. But as @sgoulas mentioned, there are additional benefits.
If you want a similar API, Jason Miller (@developit) created a tiny version (884b min+gzip): github.com/developit/redaxios. It's missing interceptors but still cool.

But with that argument, you could dismiss every frontend framework as bytes vs developer experience.

Developer experience is not to be understated. But I 100% agree with your sentiment!

Collapse
 
waylonwalker profile image
Waylon Walker

You completely missed the #1 reason I use axios... It just works everywhere.

Here is how fetch behaves in IE.

fetch in IE

Collapse
 
kosich profile image
Kostia Palchyk

To be fair, you're comparing a platform API and a library. There are fetch polyfills, like github's fetch that deliver wider support. Though if one is sharing code with nodejs -- they'll have to install another library for fetching serverside.

Collapse
 
waylonwalker profile image
Waylon Walker

Great point. I ran into some issues with a polyfill early on, and went back to axios as I am comfortable with it and really don't like opening IE if I can avoid it.

Thread Thread
 
kosich profile image
Kostia Palchyk • Edited

Totally understandable, hehe 🙂
I'm currently trying unfetch on my new project, not sure where that leads to.
And, as always, theres also a third school of thought: ky (haven't tried it).
UPD its based on fetch, so not really a 3rd, 2.5 rather...

Collapse
 
okbrown profile image
Orlando Brown • Edited

~Try using Got. npmjs.com/package/got and then compare that to Axios.~ (strike through?)

Just remembered it does not support browser. Axios FTW lol

Collapse
 
kosich profile image
Kostia Palchyk

got has a browser sibling: ky
Just discussed this in other thread:

And, as always, theres also a third school of thought: ky (haven't tried it).
UPD its based on fetch, so not really a 3rd, 2.5 rather...

Collapse
 
hemant profile image
Hemant Joshi

Cheers, also Axios got into the role due to React, I guess;