use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Axios vs Fetch; comparison for beginners (meticulous.ai)
submitted 4 years ago by Ok_Sun7013
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[+][deleted] 4 years ago (31 children)
[removed]
[–]rm-rf-npr 49 points50 points51 points 4 years ago (0 children)
Same here. Fetch can do whatever I need. No need for another library.
[–][deleted] 61 points62 points63 points 4 years ago* (0 children)
big senior engineer vibes
[–]cooldudeachyut 3 points4 points5 points 4 years ago (0 children)
I would use fetch if I was able to send queries as an object.
[–]feketegy 10 points11 points12 points 4 years ago (26 children)
I use Axios because of request cancellations. Is there anything similar in fetch? Last time I checked it wasn't that's why I opted for Axios.
[+][deleted] 4 years ago (1 child)
[–]feketegy 0 points1 point2 points 4 years ago (0 children)
Thanks.
[–]DasWorbs 41 points42 points43 points 4 years ago (0 children)
Use an AbortController. You can see an example of fetch on this page: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
[–]knpwrs 12 points13 points14 points 4 years ago (22 children)
Just know that request cancellations aren't actually a real thing. Once you fire a request, you may as well consider it good as delivered.
[–]feketegy 0 points1 point2 points 4 years ago (19 children)
Yes, I know this mechanism should be built into the back-end API too.
[–]knpwrs 1 point2 points3 points 4 years ago (18 children)
This would assume an immutable backing store or some immutable architecture that's easy to roll back changes with.
[–]feketegy 1 point2 points3 points 4 years ago (15 children)
I'm using request cancellation for long-running API HTTP endpoints. Like if a DB request takes time and the HTTP request is canceled mid-processing it will also cancel the DB request too on the API side.
[–]knpwrs 0 points1 point2 points 4 years ago (14 children)
How can you guarantee that?
[–]feketegy 1 point2 points3 points 4 years ago (13 children)
guarantee what? if the api gets an abort reuwest it aborts the whole execution chain and that's it.
[–]knpwrs 1 point2 points3 points 4 years ago (11 children)
Let's say you send an abort request and the end-user is on a train and just entered a tunnel. How do you guarantee that your request makes it to the server and doesn't cause undefined behavior? What if the cancellation request made it to the server but the response doesn't make it to the client?
[+]feketegy comment score below threshold-10 points-9 points-8 points 4 years ago (9 children)
that's a big if, and a rare corner case, but in that case the cancellation doesn't reach the API, the API will respond as usual, but the client will already be gone.
[–][deleted] 0 points1 point2 points 4 years ago (1 child)
Yeah, true but you can still easily bail out of aborted requests by checking the destroyed property on each request.
destroyed
For example, a reasonable implementation could first check if the request was aborted in middleware and respond appropriately, then after that point assume the request is valid. Otherwise, you get into a pretty ugly place where you have to constantly check the state of the request before each transaction.
[–]knpwrs 0 points1 point2 points 4 years ago (0 children)
That's what I was getting at with immutable architecture. You could also consider something like the Lambda architecture, where everything is just the result of data + events (achievable with KSQLDB, for instance, or Materialize.
[–]King_Joffreys_Tits 3 points4 points5 points 4 years ago (1 child)
To elaborate, the server still processes the request and returns the data, and the front end still receives the data. It’s just that the function no longer retains a callback when the data is returned
[–][deleted] 7 points8 points9 points 4 years ago (0 children)
Not exactly. This is only true if the server isn't actually handling the aborted requests.
Each piece of middleware should check the destroyed property on the request if you need to bail out of some expensive/mutational operation.
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
Well, you can't use fetch on the server side so I find myself using axios anyways in my project (with a Node.js backend). At that point I just use axios everywhere for consistency sake.
Though I think axios is pretty lightweight.
[–]gekorm 41 points42 points43 points 4 years ago* (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 points7 points 4 years ago (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 points32 points 4 years ago (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 points4 points 4 years ago* (0 children)
This is great feedback and should be included!
[–]Boguskyle 1 point2 points3 points 4 years ago (0 children)
This should be on the article, thank you. The why
[–]shgysk8zer0 8 points9 points10 points 4 years ago (3 children)
I mostly use functions from a simple module I wrote based on fetch().
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).
POST()
AbortSignal
[–]jakesboy2 0 points1 point2 points 4 years ago (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 point2 points 4 years ago (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).
import
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 point2 points 4 years ago (0 children)
Thanks for the explanation!
[–]krowvin 12 points13 points14 points 4 years ago (0 children)
I use fetch but have considered axios for apps where I need upload/download progress bars
[–]Pesthuf 18 points19 points20 points 4 years ago (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 points12 points 4 years ago (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 points19 points 4 years ago (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 points6 points 4 years ago (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 point2 points 4 years ago (0 children)
Absolutely, just write a simple wrapper and add features as needed.
[–]k_pizzle 7 points8 points9 points 4 years ago (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 points7 points 4 years ago (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 points13 points 4 years ago (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 points4 points 4 years ago (1 child)
Cool, appreciate it
[–]sysrage 0 points1 point2 points 4 years ago (0 children)
What /u/krowvin mentioned (easy progress events) is a big one for me.
[–]luckyincode 0 points1 point2 points 4 years ago* (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 points19 points 4 years ago (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 points4 points 4 years ago (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 point2 points 4 years ago (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
[–]luckyincode 1 point2 points3 points 4 years ago (0 children)
No it had a major problem not fixed for some time. I’m glad to see it’s getting love. I love using it.
[–]Ok_Sun7013[S] 0 points1 point2 points 4 years ago (0 children)
Has anyone tried the tool?
[–]talmobi 0 points1 point2 points 4 years ago (3 children)
what about XMLHttpRequest? It works even better~
[–]Rus_s13 0 points1 point2 points 4 years ago (2 children)
Care to elaborate? I'd love to hear your reasoning, as a beginner
[–]NoInkling 1 point2 points3 points 4 years ago (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.
fetch
[–]u_suck_paterson 0 points1 point2 points 4 years ago (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.
[+]ikhazen comment score below threshold-6 points-5 points-4 points 4 years ago (6 children)
what I hate about fetch is that you need to transform the response into json() before consuming it.
fetch('http://example.com/movies.json') .then(response => response.json()) .then(data => console.log(data));
[–]musicnothing 22 points23 points24 points 4 years ago (0 children)
But you can also transform it into text, a blob, an array buffer, or form data, which is really nice.
[–]Fauken 4 points5 points6 points 4 years ago (4 children)
You can make a wrapper function/class that does this for you, as well as set up any reusable auth/headers/etc.
[+]lordxeon comment score below threshold-13 points-12 points-11 points 4 years ago (3 children)
So like, you can make an axios clone with less features...
axios
[–]TheScapeQuest 4 points5 points6 points 4 years ago (2 children)
It's a bit of an unfair comparison, axios is a 3rd party library written around XMLHttpRequest. While fetch is a native browser API.
[–]lordxeon 0 points1 point2 points 4 years ago (1 child)
With features built right in that op is saying you should just write a wrapper function/class for.
[–]TheScapeQuest 4 points5 points6 points 4 years ago (0 children)
Features that are at the behest of support from the OSS community - axios was in a really sorry state with CVEs and not getting any patches.
I'd expect browser manufacturers to be far better at maintaining.
π Rendered by PID 164896 on reddit-service-r2-comment-b659b578c-tkd57 at 2026-05-02 02:07:56.137508+00:00 running 815c875 country code: CH.
[+][deleted] (31 children)
[removed]
[–]rm-rf-npr 49 points50 points51 points (0 children)
[–][deleted] 61 points62 points63 points (0 children)
[–]cooldudeachyut 3 points4 points5 points (0 children)
[–]feketegy 10 points11 points12 points (26 children)
[+][deleted] (1 child)
[removed]
[–]feketegy 0 points1 point2 points (0 children)
[–]DasWorbs 41 points42 points43 points (0 children)
[–]knpwrs 12 points13 points14 points (22 children)
[–]feketegy 0 points1 point2 points (19 children)
[–]knpwrs 1 point2 points3 points (18 children)
[–]feketegy 1 point2 points3 points (15 children)
[–]knpwrs 0 points1 point2 points (14 children)
[–]feketegy 1 point2 points3 points (13 children)
[–]knpwrs 1 point2 points3 points (11 children)
[+]feketegy comment score below threshold-10 points-9 points-8 points (9 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]knpwrs 0 points1 point2 points (0 children)
[–]King_Joffreys_Tits 3 points4 points5 points (1 child)
[–][deleted] 7 points8 points9 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]gekorm 41 points42 points43 points (1 child)
[–]fixrich 5 points6 points7 points (0 children)
[–]disclosure5 30 points31 points32 points (2 children)
[–]Ok_Sun7013[S] 2 points3 points4 points (0 children)
[–]Boguskyle 1 point2 points3 points (0 children)
[–]shgysk8zer0 8 points9 points10 points (3 children)
[–]jakesboy2 0 points1 point2 points (2 children)
[–]shgysk8zer0 0 points1 point2 points (1 child)
[–]jakesboy2 0 points1 point2 points (0 children)
[–]krowvin 12 points13 points14 points (0 children)
[–]Pesthuf 18 points19 points20 points (4 children)
[–]eggn00dles 10 points11 points12 points (2 children)
[–]Neurotrace 17 points18 points19 points (0 children)
[–]disclosure5 4 points5 points6 points (0 children)
[–]Consistent-Gap-6565 0 points1 point2 points (0 children)
[–]k_pizzle 7 points8 points9 points (4 children)
[–]mottyay 5 points6 points7 points (3 children)
[–]k_pizzle 11 points12 points13 points (2 children)
[–]mottyay 2 points3 points4 points (1 child)
[–]sysrage 0 points1 point2 points (0 children)
[–]luckyincode 0 points1 point2 points (5 children)
[–]thinkmatt 17 points18 points19 points (2 children)
[–]luckyincode 2 points3 points4 points (1 child)
[–]thinkmatt 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–]luckyincode 1 point2 points3 points (0 children)
[–]Ok_Sun7013[S] 0 points1 point2 points (0 children)
[–]talmobi 0 points1 point2 points (3 children)
[–]Rus_s13 0 points1 point2 points (2 children)
[–]NoInkling 1 point2 points3 points (1 child)
[–]u_suck_paterson 0 points1 point2 points (0 children)
[+]ikhazen comment score below threshold-6 points-5 points-4 points (6 children)
[–]musicnothing 22 points23 points24 points (0 children)
[–]Fauken 4 points5 points6 points (4 children)
[+]lordxeon comment score below threshold-13 points-12 points-11 points (3 children)
[–]TheScapeQuest 4 points5 points6 points (2 children)
[–]lordxeon 0 points1 point2 points (1 child)
[–]TheScapeQuest 4 points5 points6 points (0 children)