Function: withTimeout()

withTimeout<T>(target, ms, options?): Promise<T>

Races a promise against a time budget. Rejects with a TimeoutError if the promise does not settle within ms.

Note: rejection does not cancel the underlying work — pass an AbortSignal to the operation itself if it supports cancellation.

Type Parameters

T

T

Parameters

target

Promise<T>

The promise to await.

ms

number

Time budget in milliseconds.

options?

AsyncControlOptions & object

Optional message override and abort signal.

Returns

Promise<T>

A Promise resolving with the target's value.

Example

const data = await withTimeout(fetch('/api/slow'), 5000)