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
(cherry picked from commit 1e2581e57b
)
This commit is contained in:
parent
b6c0313d7e
commit
634afebe58
2 changed files with 30 additions and 0 deletions
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue