DEV Community

loading...

Discussion on: Fetch vs. Axios - comparison

Collapse
sebbdk profile image
Sebastian Vargr • Edited

We are comparing a library to a native browser method, it seems kinda like comparing an apple to an apple-tree to me. Is this comparison valid?

In a performance-based world, the native method always wins out because of the library weight.

As for interceptors, consider using a service-worker for that kind of operation.
Or, wrap your fetch method, in an "interceptedFetch" method.

Also, there are no async examples here. Fetch is soooo' much more fun and verbose to use when coupled with async functions.

async function getTheThings() {
  try {
    const request = await fetch('....my-url.json', ...);
    const data = await request.json();

   // ...do something with data, return, format etc.
  } catch(e) {
  // handle error
  // or throw a new custom that the callee can be setup to handle
  }
}
Enter fullscreen mode Exit fullscreen mode

All promises work interchangeably with async. :)
And you only need the expensive polyfills if you support legacy browsers.