1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00
denoland-deno/ext/node/polyfills/_util/async.ts
Kenta Moriuchi f0a3d20642
fix(runtime): use more null proto objects again (#25040)
proceed with #23921

This PR is a preparation for
https://github.com/denoland/deno_lint/pull/1307

---------

Signed-off-by: Kenta Moriuchi <moriken@kimamass.com>
Co-authored-by: Luca Casonato <hello@lcas.dev>
2024-09-06 12:52:59 +02:00

34 lines
942 B
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is vendored from std/async/delay.ts
// (with some modifications)
import { primordials } from "ext:core/mod.js";
const {
Promise,
PromiseReject,
} = primordials;
import { clearTimeout, setTimeout } from "ext:deno_web/02_timers.js";
/** Resolve a Promise after a given amount of milliseconds. */
export function delay(
ms: number,
options: { signal?: AbortSignal } = { __proto__: null },
): Promise<void> {
const { signal } = options;
if (signal?.aborted) {
return PromiseReject(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 });
});
}