-
Notifications
You must be signed in to change notification settings - Fork 576
/
delay.ts
39 lines (37 loc) · 1.26 KB
/
delay.ts
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
export interface DelayOptions {
signal?: AbortSignal;
/** Indicates whether the process should continue to run as long as the timer exists. This is `true` by default. */
persistent?: boolean;
}
/* Resolves after the given number of milliseconds. */
export function delay(ms: number, options: DelayOptions = {}): Promise<void> {
const { signal, persistent } = options;
if (signal?.aborted) {
return Promise.reject(new DOMException("Delay was aborted.", "AbortError"));
}
return new Promise((resolve, reject) => {
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 });
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");
}
}
});
}