withTimeout es-toolkit
Sets a time limit on an asynchronous function, throwing a TimeoutError if it doesn't complete within the specified time.
Usage
withTimeout(run, ms, options?)
Use withTimeout when you want to set a timeout on an asynchronous task. If the Promise doesn't complete within the specified time, it's rejected with a TimeoutError, preventing indefinite waiting.
You can also use it when you want to set a time limit on database queries.
You can also use it when you want to receive only the fastest response among multiple API calls.
You can pass an AbortSignal to cancel the timeout. Aborting it lifts the time limit, so run is awaited without a deadline. It does not reject the returned Promise or abort run itself. Pass the same signal into run if you also want to cancel the underlying work.
Parameters
The asynchronous function to execute.
The amount of time in milliseconds until the timeout occurs.
Timeout options.
signal(AbortSignal, optional): An AbortSignal to cancel the timeout. When aborted, the time limit is lifted andrunis awaited without a deadline.
Returns
Returns the result of the given asynchronous function, or a Promise that's rejected with a TimeoutError if it times out.
Errors
Throws TimeoutError if not completed within the specified time.
Source: es-toolkit
Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.