1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -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
This commit is contained in:
Marvin Hagemeister 2024-07-31 12:00:01 +02:00 committed by GitHub
parent cd59fc53a5
commit 1e2581e57b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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;