Skip to main content
ofetch provides built-in timeout support that automatically cancels requests that take too long to complete.

Basic Timeout

Set a timeout in milliseconds using the timeout option:
number
Request timeout in milliseconds. The request will be automatically aborted if it takes longer than this value.
If the request exceeds the timeout, an error is thrown:

How Timeout Works

ofetch uses the modern AbortSignal.timeout() API to implement timeouts:
When the timeout expires, the request is aborted with a TimeoutError.
Timeout functionality requires AbortSignal.timeout() support, which is available in Node.js 17.3+ and modern browsers.

Combining Timeout with AbortSignal

You can combine the timeout option with your own AbortSignal:
ofetch uses AbortSignal.any() to combine multiple abort signals:
The request will be aborted when either signal triggers.

Timeout with Retry

When using timeout with retry, each retry attempt gets its own timeout:
In this example:
  • First attempt: 3 second timeout
  • Wait 1 second
  • Second attempt: 3 second timeout
  • Wait 1 second
  • Third attempt: 3 second timeout
Total maximum time: (3 + 1 + 3 + 1 + 3) = 11 seconds
Timeout does NOT prevent retries. If a request times out and retry is configured, ofetch will retry the request. Set retry: false or retry: 0 to disable retries on timeout.

Disabling Retry on Timeout

If you want to timeout without retrying:

Different Timeouts for Different Requests

You can set different timeouts for different types of requests:

Global Timeout Configuration

Set a default timeout for all requests using $fetch.create():

Timeout Error Handling

Distinguish between timeout errors and other errors:

Timeout with Loading States

Combine timeout with loading states in UI:

Complete Example