Use Axios, Its compatable!

Other ways to write fetches???

Angelo Poole
2 min readNov 22, 2020
Photo by James Haworth on Unsplash

While following some courses and writing some node.js programs I ran into something that I'm not familiar with as a new software engineer. In the course, the instructor was using axios.js to write out functions that would return promises from their API. This was super interesting to me because for a bit of time I thought that there was only one way to write such functions, which was through javascript’s built-in API, Fetch.

Axios is an npm package that is described as a promise-based HTTP client for the browser and node.js. It works similarly to fetch which is an API for javaScript that allows the creation of an interface for accessing and manipulating parts of the HTTP pipeline, such as requests and response.

example of a fetch call vs Axios call

While these can both do similar things, Axios takes the cake in terms of simplicity. For instance, using Axios we are able to skip the parsing of information to JSON because Axios will automatically do that for us.

We are also given the ability to use request cancelation and interceptors.
Interceptors give us the ability to, you guessed it, intercept requests and responses before they hit a then or catch statement. This allows us to examine or change HTTP requests from our application to the server allowing us to write less code as we don't have to write a separate request for every HTTP request.
Cancellation is the ability to cancel a request using a cancel token. This allows us to control when and if a request should be canceled. We are able to cancel multiple requests with a single cancelation token.

These are just two of the things that make Axios a desirable client for promise handling. We haven't even gotten into Axios’s ability to fetch multiple things at once with simple code, or its ability to work on most modern browsers (Yes even on windows explorer). I'd definitely recommend trying it out the next time you decide to write a fetch request, you might just find the next package you’ll use for every project you use 😎.

--

--

Angelo Poole

Software engineer, Graduate of Flatiron school. Currently volunteering! Looking to talk to fellow engineers, please send me a message!