@rtorcato/api-timeout
v0.1.0
Published
Framework-agnostic per-request timeout for Node API projects — race a promise against a deadline and reject with a ServiceUnavailableError.
Maintainers
Readme
@rtorcato/api-timeout
Framework-agnostic per-request timeout. Races a promise against a deadline and
rejects with a ServiceUnavailableError (503) from
@rtorcato/api-errors.
import { withTimeout } from '@rtorcato/api-timeout'
// Resolves with the fetch result, or rejects with a 503 after 2s.
const user = await withTimeout(fetchUser(id), 2000)API
withTimeout(promise, ms)
Returns a promise that:
- resolves/rejects with
promise's own result if it settles withinms; - rejects with a
ServiceUnavailableError(503) oncemselapses.
The timer is always cleared, so a promise that settles first leaves nothing dangling.
On timeout the losing promise keeps running — JS has no cancellation. Its result is discarded; make the wrapped work idempotent/abortable if that matters.
For HTTP middleware, use the framework adapters
@rtorcato/api-timeout-express
and
@rtorcato/api-timeout-hono.
License
MIT
