timeout
asyncCreates a promise that rejects if not resolved within the specified timeout.
Signature
timeout<T>(promise: Promise<T>, ms: number, message?: string): Promise<T>Parameters
| Name | Type | Description |
|---|---|---|
promise | Promise<T> | The promise to add timeout to |
ms | number | The timeout in milliseconds |
message? | string | Custom 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