Search code examples
javascriptexceptionerror-handlingfetch-api

Don't report a fetch timeout as an uncaught exception


I use:

fetch("/test", { signal: AbortSignal.timeout(2000) })
    .then(r => r.json())
    .then(r => { console.log(r["data"]) });

with the method from Fetch API request timeout? to abort the request after 2 seconds if no response.

In the case of a timeout, I have a JS error in the console:

Uncaught (in promise) DOMException: The operation timed out.

What's the simplest way to avoid having this reported as an error?

I would like the timeouts to be silently ignored.

Is this possible with fetch("/test", { signal: AbortSignal.timeout(2000) })? I specifically wanted to use this solution instead of more complex solutions of Fetch API request timeout?.


Solution

  • As mentioned in a comment, the solution is to add

    .catch(error => {});
    

    I was doing it wrong before.