2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-03-01 04:25:50 +00:00
|
|
|
// This module is browser compatible.
|
2021-08-17 05:04:21 +00:00
|
|
|
|
2023-12-01 02:19:22 +00:00
|
|
|
/** Options for {@linkcode delay}. */
|
2021-08-17 05:04:21 +00:00
|
|
|
export interface DelayOptions {
|
2022-11-25 11:40:23 +00:00
|
|
|
/** Signal used to abort the delay. */
|
2021-08-17 05:04:21 +00:00
|
|
|
signal?: AbortSignal;
|
2022-11-25 11:40:23 +00:00
|
|
|
/** Indicates whether the process should continue to run as long as the timer exists.
|
|
|
|
*
|
|
|
|
* @default {true}
|
|
|
|
*/
|
2022-08-22 13:09:53 +00:00
|
|
|
persistent?: boolean;
|
2021-08-17 05:04:21 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 11:40:23 +00:00
|
|
|
/**
|
2023-12-01 02:19:22 +00:00
|
|
|
* Resolve a {@linkcode Promise} after a given amount of milliseconds.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* @example
|
2023-12-01 02:19:22 +00:00
|
|
|
* ```ts
|
2022-11-25 11:40:23 +00:00
|
|
|
* import { delay } from "https://deno.land/std@$STD_VERSION/async/delay.ts";
|
|
|
|
*
|
|
|
|
* // ...
|
|
|
|
* const delayedPromise = delay(100);
|
|
|
|
* const result = await delayedPromise;
|
|
|
|
* // ...
|
|
|
|
* ```
|
|
|
|
*
|
2024-01-24 20:48:48 +00:00
|
|
|
* To allow the process to continue to run as long as the timer exists.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2023-12-01 02:19:22 +00:00
|
|
|
* ```ts
|
2022-11-25 11:40:23 +00:00
|
|
|
* import { delay } from "https://deno.land/std@$STD_VERSION/async/delay.ts";
|
|
|
|
*
|
|
|
|
* // ...
|
|
|
|
* await delay(100, { persistent: false });
|
|
|
|
* // ...
|
|
|
|
* ```
|
|
|
|
*/
|
2021-08-17 05:04:21 +00:00
|
|
|
export function delay(ms: number, options: DelayOptions = {}): Promise<void> {
|
2024-04-11 21:23:54 +00:00
|
|
|
const { signal, persistent = true } = options;
|
2023-07-14 05:03:05 +00:00
|
|
|
if (signal?.aborted) return Promise.reject(signal.reason);
|
2022-08-24 01:21:57 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-08-17 05:04:21 +00:00
|
|
|
const abort = () => {
|
|
|
|
clearTimeout(i);
|
2023-07-14 05:03:05 +00:00
|
|
|
reject(signal?.reason);
|
2021-08-17 05:04:21 +00:00
|
|
|
};
|
|
|
|
const done = () => {
|
|
|
|
signal?.removeEventListener("abort", abort);
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
const i = setTimeout(done, ms);
|
|
|
|
signal?.addEventListener("abort", abort, { once: true });
|
2022-08-22 13:09:53 +00:00
|
|
|
if (persistent === false) {
|
2022-09-07 11:19:59 +00:00
|
|
|
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");
|
|
|
|
}
|
2022-08-22 13:09:53 +00:00
|
|
|
}
|
2021-08-17 05:04:21 +00:00
|
|
|
});
|
2020-05-09 12:34:47 +00:00
|
|
|
}
|