1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

fix(node/timers/promises): add scheduler APIs (#24802)

This PR adds the experimental `scheduler` APIs in Node's
`timers/promises` module. See
https://nodejs.org/api/timers.html#timerspromisesschedulerwaitdelay-options

Fixes https://github.com/denoland/deno/issues/24800

(cherry picked from commit 1e2581e57b)
This commit is contained in:
Marvin Hagemeister 2024-07-31 12:00:01 +02:00 committed by crowlkats
parent b6c0313d7e
commit 634afebe58
No known key found for this signature in database
GPG key ID: A82C9D461FC483E8
2 changed files with 30 additions and 0 deletions

View file

@ -6,6 +6,13 @@ export const setTimeout = promisify(timers.setTimeout),
setImmediate = promisify(timers.setImmediate),
setInterval = promisify(timers.setInterval);
export const scheduler = {
async wait(delay: number, options?: { signal?: AbortSignal }): Promise<void> {
return await setTimeout(delay, undefined, options);
},
yield: setImmediate,
};
export default {
setTimeout,
setImmediate,

View file

@ -66,6 +66,29 @@ Deno.test("[node/timers/promises setTimeout]", () => {
return p;
});
Deno.test("[node/timers/promises scheduler.wait]", async () => {
const { scheduler } = timersPromises;
let resolved = false;
timers.setTimeout(() => (resolved = true), 20);
const p = scheduler.wait(20);
assert(p instanceof Promise);
await p;
assert(resolved);
});
Deno.test("[node/timers/promises scheduler.yield]", async () => {
const { scheduler } = timersPromises;
let resolved = false;
timers.setImmediate(() => resolved = true);
const p = scheduler.yield();
assert(p instanceof Promise);
await p;
assert(resolved);
});
// Regression test for https://github.com/denoland/deno/issues/17981
Deno.test("[node/timers refresh cancelled timer]", () => {
const { setTimeout, clearTimeout } = timers;