DEV Community

Discussion on: Fetch-22

Collapse
 
alfredosalzillo profile image
Alfredo Salzillo

Can you show the benefit of the callback and errorHandler and parser over the then/catch of promise ?

Collapse
 
madsstoumann profile image
Mads Stoumann • Edited

I think it's just an easier syntax, parsing one init-object with these extra options. The result itself is a Promise, the start/stop callback is called on init, and in the .finally()-block. The errorHandler takes care of "AbortErrors" (timeout) as well:

catch(error => {
      if (error.name === 'AbortError') {
        errorHandler({
          name: 'FetchError',
          message: `Timeout after ${timeout} milliseconds.`,
          response: '',
          status: 524
        });
      } else {
        errorHandler(error);
      }
Enter fullscreen mode Exit fullscreen mode