deno.land / std@0.224.0 / async / delay.ts

View Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
/** Options for {@linkcode delay}. */export interface DelayOptions { /** Signal used to abort the delay. */ signal?: AbortSignal; /** Indicates whether the process should continue to run as long as the timer exists. * * @default {true} */ persistent?: boolean;}
/** * Resolve a {@linkcode Promise} after a given amount of milliseconds. * * @example * ```ts * import { delay } from "https://deno.land/std@$STD_VERSION/async/delay.ts"; * * // ... * const delayedPromise = delay(100); * const result = await delayedPromise; * // ... * ``` * * To allow the process to continue to run as long as the timer exists. * * ```ts * import { delay } from "https://deno.land/std@$STD_VERSION/async/delay.ts"; * * // ... * await delay(100, { persistent: false }); * // ... * ``` */export function delay(ms: number, options: DelayOptions = {}): Promise<void> { const { signal, persistent = true } = options; if (signal?.aborted) return Promise.reject(signal.reason); return new Promise((resolve, reject) => { const abort = () => { clearTimeout(i); reject(signal?.reason); }; const done = () => { signal?.removeEventListener("abort", abort); resolve(); }; const i = setTimeout(done, ms); signal?.addEventListener("abort", abort, { once: true }); if (persistent === false) { try { // @ts-ignore For browser compatibility Deno.unrefTimer(i); } catch (error) { if (!(error instanceof ReferenceError)) { throw error; } console.error("`persistent` option is only available in Deno"); } } });}
std

Version Info

Tagged at
6 months ago