timeout

async

Creates a promise that rejects if not resolved within the specified timeout.

Signature

timeout<T>(promise: Promise<T>, ms: number, message?: string): Promise<T>

Parameters

NameTypeDescription
promisePromise<T>The promise to add timeout to
msnumberThe timeout in milliseconds
message?stringCustom error message

Returns

Promise<T> - Returns the promise result or throws TimeoutError

Examples

Timeout API call

import { timeout } from 'dashlite'

const result = await timeout(
  fetch('https://api.example.com/data'),
  5000
)

Custom error message

import { timeout } from 'dashlite'

try {
  await timeout(slowOperation(), 3000, 'Operation timed out')
} catch (error) {
  console.error(error.message)
}

Available since version 1.2.0