Skip to main content
Module

x/timers/mod.ts>pTimeout

All timing functions you need - long and async timeouts, intervals, promise timeouts and more
Latest
function pTimeout
import { pTimeout } from "https://deno.land/x/timers@v0.2.3/mod.ts";

Timeout a promise after a specified amount of time.

Examples

Example 1

import pTimeout from 'https://deno.land/x/timers@v0.2.0/mod.ts'
const delayedPromise = new Promise(resolve => setTimeout(resolve, 500))
await pTimeout({
    promise: delayedPromise,
    milliseconds: 50
})
//=> [TimeoutError: Promise timed out after 50 milliseconds]

Parameters

promise: Promise<T> | ((signal: AbortSignal) => Promise<T>)
  • Promise to decorate.
delay: number | string
  • Milliseconds before timing out.
optional
options: PTimeoutOptions<T> = [UNSUPPORTED]
  • Do something other than rejecting with an error on timeout. You could for example retry.

Returns

A decorated options.promise that times out after options.milliseconds time. It has a .clear() method that clears the timeout.