Axios
and fetch()
make requests to the server to get or post data in an asynchronous manner. In this shot, we will compare axios
and fetch()
and see how they can be used to perform different tasks.
The data in fetch()
needs to be stringified
, whereas the data in axios
is a simple JavaScript object.
The URL is passed as an argument to fetch()
, whereas the URL is set in the options object for axios
.
The response object in fetch
is also made into a string and needs an additional step to convert it into JSON format. However, in axios
, the response object is already in the JSON format.
Fetch
uses an AbortController
object that allows us to abort the request using the signal
property (read-only) of AbortController
.The USP of axios
is its wide browser support. Old browsers like IE11 can also run axios
without any issue. Fetch
only supports Chrome 42+, Firefox 39+, Edge 14+, and Safari 10.1+.
To make multiple requests, fetch
provides the Promise.all()
method.
Promise.all([
fetch(URL, options),
fetch(URL, options)
])
.then([obj1, obj2] => {
console.log(obj1);
console.log(obj2);
})