5 ms·
I haven't seen XMLHttpRequest used directly in non-legacy code in quite some time. If there is any battle, it's between fetch and axios[1], and fetch is winning
by cyco130 5y ago
I haven't seen XMLHttpRequest used directly in non-legacy code in quite some time. If there is any battle, it's between fetch and axios[1], and fetch is winning by virtue of being standard. Node.js also decided to implement it.[2]
[1] https://github.com/axios/axios https://github.com/axios/axios
[2] https://blog.logrocket.com/fetch-api-node-js/ https://blog.logrocket.com/fetch-api-node-js/
- redox99 5y agoWhat do you mean by winning? As in usage? Axios has many nice features (such as interceptors) and I think is just better than fetch, other than the fact that you need to add a few KB to your bundle of course.
- cyco130 4y agoYes, I meant usage. I agree axios has many nice features but fetch comes with 90% of what you need for free. Also, its API is spreading on the "other side" too. Service workers, worker runtimes (Cloudflare etc.) and Deno all use the fetch API for handling requests (as opposed to sending them).
- Jasper_ 5y agoI still write XMLHttpRequest and see no reason to switch. I depend on things like progress notifications and cancelations which fetch has iffy support for. https://github.com/magcius/noclip.website/blob/master/src/DataFetcher.ts#L96 https://github.com/magcius/noclip.website/blob/master/src/Da...
- 6510 4y agoSame here. Look how clean it is! with(console){ with(new XMLHttpRequest()){ onload=()=>log(responseText); onerror=error; open("GET", "https://.."); send(); } } I'm only half joking.
- cyco130 4y agoMy... eyes... For people less familiar with the subject: console.log(await fetch("https:/...").then(r => r.text())) I agree progress reporting needs improvement though.
- easrng 4y agoProgress is possible with fetch, you just need to read from the stream yourself. And since you're working with the stream, you can also do things like parse newline-delimited JSON as it downloads instead of all at once at the end. https://fetch-progress-demo.glitch.me/ https://fetch-progress-demo.glitch.me/