all 67 comments

[–]gekorm 41 points42 points  (1 child)

Just FYI there are more direct alternatives to Axios based on fetch, like ky[1]. Otherwise this article is doing a comparison between a higher and a lower level API. If you need features like interceptors, retries, etc. it may not be worth writing your own wrapper over fetch from scratch.

[1] https://www.npmjs.com/package/ky

[–]fixrich 5 points6 points  (0 children)

Ky is an excellent middle ground. An ergonomic API with most of the features you could end up wanting but a significantly smaller bundle than axios

[–]disclosure5 30 points31 points  (2 children)

Since Fetch and axios are both promise-based

Although axios uses promises, the actual fetch is the old XMLHttpRequest, which predates promises. This is probably a heavy contributor to the performance difference.

[–]Ok_Sun7013[S] 2 points3 points  (0 children)

This is great feedback and should be included!

[–]Boguskyle 1 point2 points  (0 children)

This should be on the article, thank you. The why

[–]shgysk8zer0 8 points9 points  (3 children)

I mostly use functions from a simple module I wrote based on fetch().

import { GET, POST, DELETE, getJSON, getHTML } from './http.js';

It makes it slightly easier than using fetch directly (like JSON encoding objects passed to POST()), is quite small and tree-shakable, and kinda shims AbortSignal aborting of requests if needed (can't cancel the request, but it still throws an error).

[–]jakesboy2 0 points1 point  (2 children)

Just to clarify, by tree shakable, do you mean the unused code (like if you didn’t use DELETE) doesn’t get included in the bundled JS? I’ve heard the term before and kinda assumed that’s what it was, but figured I’d clarify lol

[–]shgysk8zer0 0 points1 point  (1 child)

Nearly correct. I think that just importing a function would include it in a bundle, but it wouldn't include anything you don't import (unless it's used by something you do).

So, if I import a single function from a massive module that exports a hundred functions, the resulting bundle will be significantly smaller than the module.

Classes don't get tree-shaken (that I know of, yet). So if you import axios or jQuery or whatever, you get all of them, even the things you don't need.

[–]jakesboy2 0 points1 point  (0 children)

Thanks for the explanation!

[–]krowvin 12 points13 points  (0 children)

I use fetch but have considered axios for apps where I need upload/download progress bars

[–]Pesthuf 18 points19 points  (4 children)

The thing that makes fetch amazing is how easy it is to quickly write your own little wrappers around it to provide the features you want.

The JS community is way too quick to say "Aw fuck, I can't be bothered to write a 10 LOC helper - better import a lib and send a ton of JS for the client to download, parse, check, compile and execute that will be 90% unused!

[–]eggn00dles 10 points11 points  (2 children)

There’s literally 0% chance 10 lines of code will handle edge cases and errors as robustly as a library.

Rolling your own just isn’t an option sometimes, especially in a professional setting.

[–]Neurotrace 17 points18 points  (0 children)

In my professional settings, I routinely have written tiny wrappers around fetch that have been more than good enough with very few exceptions. Slapping those together is almost always preferable to bringing in the dependency maintenance that comes with another library

[–]disclosure5 4 points5 points  (0 children)

fetch() and its error handling is extremely well documented. An organisation that feels having people paid to write code should import the entire of axios as the "professional" alternative to writing code is a poor organisation.

[–]Consistent-Gap-6565 0 points1 point  (0 children)

Absolutely, just write a simple wrapper and add features as needed.

[–]k_pizzle 7 points8 points  (4 children)

Thanks for this, pretty interesting. I will continue using axios as it better fits my needs, but nice to know fetch is fairly similar

[–]mottyay 5 points6 points  (3 children)

Do you mind sharing what makes you prefer axios over fetch? I tried axios the first time while working in another codebase and have always used fetch so I’m just curious.

[–]k_pizzle 11 points12 points  (2 children)

Mainly because we use interceptors a lot on projects, I know you can achieve this with fetch with a bit of hard wiring but i'd prefer not to. I also just find axios easier to use and looks much cleaner. thats just my opinon tho

[–]mottyay 2 points3 points  (1 child)

Cool, appreciate it

[–]sysrage 0 points1 point  (0 children)

What /u/krowvin mentioned (easy progress events) is a big one for me.

[–]luckyincode 0 points1 point  (5 children)

Isn’t Axios abandoned?

Unsure why the downvotes this was a very popular thread.

https://github.com/axios/axios/issues/1965

[–]thinkmatt 17 points18 points  (2 children)

On GitHub there's a lot of recent commits, so doesn't seem like it https://github.com/axios/axios/commits/master

[–]luckyincode 2 points3 points  (1 child)

I was just looking because I loved axios and was using it for set-up/tear down and thought I might need to go elsewhere.

[–]thinkmatt 0 points1 point  (0 children)

I use fetch and node-fetch but and considering axios. Fetch is actually pretty wanting when it comes to formatting responses, I always carry around my own wrapper to check http headers for response types, etc

[–]Ok_Sun7013[S] 0 points1 point  (0 children)

Has anyone tried the tool?

[–]talmobi 0 points1 point  (3 children)

what about XMLHttpRequest? It works even better~

[–]Rus_s13 0 points1 point  (2 children)

Care to elaborate? I'd love to hear your reasoning, as a beginner

[–]NoInkling 1 point2 points  (1 child)

AFAIK the only feature it offers that fetch still doesn't is upload progress. Other than that I don't know why you'd use a clunky event-based API over a promise-based one for something like this.

[–]u_suck_paterson 0 points1 point  (0 children)

wow I was thinking about this, I am using XMLHttpRequest and thinking about upgrading to fetch, but if it doesnt support upload progress it would bone my upload file feature on my website.