Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

std/async/delay.ts

Deno standard library
Go to Latest
File
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
export interface DelayOptions { signal?: AbortSignal;}
/* Resolves after the given number of milliseconds. */export function delay(ms: number, options: DelayOptions = {}): Promise<void> { const { signal } = options; if (signal?.aborted) { return Promise.reject(new DOMException("Delay was aborted.", "AbortError")); } return new Promise((resolve, reject): void => { const abort = () => { clearTimeout(i); reject(new DOMException("Delay was aborted.", "AbortError")); }; const done = () => { signal?.removeEventListener("abort", abort); resolve(); }; const i = setTimeout(done, ms); signal?.addEventListener("abort", abort, { once: true }); });}