Skip to content

Function: timeout()

ts
function timeout<T>(promise, ms): Promise<T>;

Rejects when promise does not settle within ms.

Type Parameters

Type Parameter
T

Parameters

ParameterType
promisePromise<T>
msnumber

Returns

Promise<T>

Example

ts
const result = await timeout(Promise.resolve('ok'), 1000)
result // => 'ok'
try {
  await timeout(new Promise(() => {}), 50)
} catch (e) {
  e.message // => 'Request timeout'
}

Since

1.0.0

Released under the MIT License.