AbortSignal.timeout()
Timeouts and deadlines
A call without a timeout waits as long as the other side lets it, holding a connection, a worker, and the user’s patience the whole time. Give every outbound call a timeout. For a request that fans out to several calls, give it a deadline: one time budget for the whole request that every downstream call draws from.
Use it for
- Every HTTP, database, and queue call that leaves the process.
- Passing the remaining budget downstream, so a service does not start work its caller has already given up on.
Watch for
- Library defaults are often unlimited or minutes long. fetch() has no timeout option of its own; without a signal it waits on the platform’s limits.
- A timeout does not mean the work did not happen. The server may finish the charge after the client stopped waiting, which is why retries need idempotency keys.
- Retries multiply time. Three attempts with a 10-second timeout is a 30-second request unless all attempts share one deadline.
const res = await fetch(url, {
signal: AbortSignal.timeout(3_000), // fail after 3 s, not "eventually"
})
// One deadline for the whole request, shared by every call in it
const deadline = AbortSignal.timeout(8_000)
const [user, plan] = await Promise.all([
fetch(userUrl, { signal: deadline }),
fetch(planUrl, { signal: deadline }),
])