Timeouts
Use case
Abort requests that take too long, either at the client level or per request.
Smallest working example
import { createClient } from '@parcely/core'
const http = createClient({
baseURL: 'https://api.example.com',
timeout: 5000, // 5 seconds for all requests
})
// Override per request
await http.get('/slow-endpoint', { timeout: 30000 })
How it works
parcely creates an AbortSignal.timeout(ms) and combines it with any user-provided signal via AbortSignal.any. When the timeout fires, the request throws HttpError with code: 'ERR_TIMEOUT'.
import { isHttpError } from '@parcely/core'
try {
await http.get('/slow')
} catch (err) {
if (isHttpError(err) && err.code === 'ERR_TIMEOUT') {
console.log('Request timed out')
}
}
Axios equivalent
// axios:
await http.get('/slow', { timeout: 5000 })
// parcely (identical):
await http.get('/slow', { timeout: 5000 })
In axios, the timeout error has code: 'ECONNABORTED'. In parcely it is code: 'ERR_TIMEOUT'.
Notes and gotchas
- Timeout is in milliseconds.
- Timers are cleaned up in
finally-- no leaked timers on success or other errors. - If both
timeoutandsignalare provided, whichever fires first aborts the request. A user signal abort producesERR_ABORTED; a timeout producesERR_TIMEOUT. - A
timeoutof0orundefinedmeans no timeout.