Skip to main content

@rtorcato/api-timeout v0.1.0

Interfaces​

TimeoutOptions​

Defined in: index.ts:3

Properties​

ms​

ms: number

Defined in: index.ts:5

Deadline in milliseconds before the operation is considered timed out.

Functions​

withTimeout()​

withTimeout<T>(promise, ms): Promise<T>

Defined in: index.ts:23

Race promise against a deadline. If ms elapses before it settles, reject with a ServiceUnavailableError (503); otherwise pass through the promise's own resolution or rejection. The timer is always cleared, so a promise that settles first never leaves a dangling handle.

Framework-agnostic — wrap it for Express/Hono in a dedicated adapter package.

ponytail: on timeout the losing promise keeps running in the background (there is no cancellation in JS). Its result is discarded; make the wrapped work idempotent/abortable yourself if that matters.

Example​

const data = await withTimeout(fetchUser(id), 2000)

Type Parameters​

T​

T

Parameters​

promise​

Promise<T>

ms​

number

Returns​

Promise<T>