std/async/delay.ts
Asher Gomez 0b2497f16e
fix: update codebase to work with Deno RC (#6018)
* fix: update codebase to work with Deno RC

* work

* fix

* fix

* fix

* fixes

* work

* update

* fixes

* fix

* revert
2024-09-20 09:29:31 +10:00

74 lines
2.0 KiB
TypeScript

// 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.
*
* @throws {DOMException} If the optional signal is aborted before the delay
* duration, and `signal.reason` is undefined.
* @param ms Duration in milliseconds for how long the delay should last.
* @param options Additional options.
*
* @example Basic usage
* ```ts no-assert
* import { delay } from "@std/async/delay";
*
* // ...
* const delayedPromise = delay(100);
* const result = await delayedPromise;
* // ...
* ```
*
* @example Disable persistence
*
* Setting `persistent` to `false` will allow the process to continue to run as
* long as the timer exists.
*
* ```ts no-assert ignore
* import { delay } from "@std/async/delay";
*
* // ...
* 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;
}
// deno-lint-ignore no-console
console.error("`persistent` option is only available in Deno");
}
}
});
}