Skip to content

timeout

Create a promise that rejects after some time

246 bytes

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 TimeoutError
await
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.

@seehttps://radashi.js.org/reference/async/timeout

@example

// Reject after 1000 milliseconds with default message "timeout"
await timeout(1000)
// Reject after 1000 milliseconds with a custom message
await timeout(1000, "Optional message")
// Reject after 1000 milliseconds with a custom error
await timeout(1000, () => new Error("Custom error"))
// Example usage with Promise.race to set a timeout for an asynchronous task
await Promise.race([
someAsyncTask(),
timeout(1000, "Optional message"),
])

@version12.3.0

timeout
(1000)
// Rejects after 1 second with a custom TimeoutError message
await
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.

@seehttps://radashi.js.org/reference/async/timeout

@example

// Reject after 1000 milliseconds with default message "timeout"
await timeout(1000)
// Reject after 1000 milliseconds with a custom message
await timeout(1000, "Optional message")
// Reject after 1000 milliseconds with a custom error
await timeout(1000, () => new Error("Custom error"))
// Example usage with Promise.race to set a timeout for an asynchronous task
await Promise.race([
someAsyncTask(),
timeout(1000, "Optional message"),
])

@version12.3.0

timeout
(1000, 'Custom timeout message')
// Rejects after 1 second with a custom error type
await
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.

@seehttps://radashi.js.org/reference/async/timeout

@example

// Reject after 1000 milliseconds with default message "timeout"
await timeout(1000)
// Reject after 1000 milliseconds with a custom message
await timeout(1000, "Optional message")
// Reject after 1000 milliseconds with a custom error
await timeout(1000, () => new Error("Custom error"))
// Example usage with Promise.race to set a timeout for an asynchronous task
await Promise.race([
someAsyncTask(),
timeout(1000, "Optional message"),
])

@version12.3.0

timeout
(1000, () => new
var Error: ErrorConstructor
new (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.

@seehttps://radashi.js.org/reference/async/sleep

@example

await sleep(1000)

@version12.1.0

sleep
(10_000)
return 'Task completed'
}
// Race between the async task and a timeout of 1 second
await
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.

@paramvalues An array of Promises.

@returnsA new Promise.

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.

@seehttps://radashi.js.org/reference/async/timeout

@example

// Reject after 1000 milliseconds with default message "timeout"
await timeout(1000)
// Reject after 1000 milliseconds with a custom message
await timeout(1000, "Optional message")
// Reject after 1000 milliseconds with a custom error
await timeout(1000, () => new Error("Custom error"))
// Example usage with Promise.race to set a timeout for an asynchronous task
await Promise.race([
someAsyncTask(),
timeout(1000, "Optional message"),
])

@version12.3.0

timeout
(1000, 'Task took too long')])