timeout
Create a promise that rejects after some time
Usage
The timeout function creates a promise that rejects after a specified delay, with an optional custom error message or error function.
The default error is a TimeoutError with the message “Operation timed out”.
import * as import _
_ from 'radashi'
// Rejects after 1 second with a default TimeoutErrorawait import _
_.timeout<Error>(ms: number, error?: string | (() => Error) | undefined): Promise<never>export timeout
The timeout function creates a promise that rejects after a
specified delay, with an optional custom error message or error
function.
timeout(1000)
// Rejects after 1 second with a custom TimeoutError messageawait import _
_.timeout<Error>(ms: number, error?: string | (() => Error) | undefined): Promise<never>export timeout
The timeout function creates a promise that rejects after a
specified delay, with an optional custom error message or error
function.
timeout(1000, 'Custom timeout message')
// Rejects after 1 second with a custom error typeawait import _
_.timeout<Error>(ms: number, error?: string | (() => Error) | undefined): Promise<never>export timeout
The timeout function creates a promise that rejects after a
specified delay, with an optional custom error message or error
function.
timeout(1000, () => new var Error: ErrorConstructornew (message?: string) => Error
Error('Custom error'))Example with Promise.race
One of the most useful ways to use _.timeout with Promise.race to set a timeout for an asynchronous operation.
import * as import _
_ from 'radashi'
const const someAsyncTask: () => Promise<string>
someAsyncTask = async () => { await import _
_.function sleep(milliseconds: number): Promise<void>export sleep
Create a promise that resolves after a given amount of time.
sleep(10_000) return 'Task completed'}
// Race between the async task and a timeout of 1 secondawait var Promise: PromiseConstructor
Represents the completion of an asynchronous operation
Promise.PromiseConstructor.race<[Promise<string>, Promise<never>]>(values: [Promise<string>, Promise<never>]): Promise<string> (+1 overload)
Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
or rejected.
race([const someAsyncTask: () => Promise<string>
someAsyncTask(), import _
_.timeout<Error>(ms: number, error?: string | (() => Error) | undefined): Promise<never>export timeout
The timeout function creates a promise that rejects after a
specified delay, with an optional custom error message or error
function.
timeout(1000, 'Task took too long')])