Skip to content

withTimeout

withTimeout<T>(fn, timeoutMs, signal?): Promise<T>

Defined in: utils/timing.ts:97

Execute an async function with a timeout.

T

() => Promise<T>

Async function to execute

number

Timeout in milliseconds

AbortSignal

Optional AbortSignal for early cancellation

Promise<T>

Promise that resolves with the function result or rejects on timeout

Error with “Operation timed out” message if timeout is exceeded

const result = await withTimeout(
() => fetch("https://api.example.com/data"),
5000
);
// With abort signal
const controller = new AbortController();
const result = await withTimeout(
() => longRunningTask(),
30000,
controller.signal
);