lib: use `Promise.withResolvers()` in timers

PR-URL: https://github.com/nodejs/node/pull/55720
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
pull/56040/head
Yagiz Nizipli 2024-11-06 17:17:31 +00:00 committed by Ruy Adorno
parent 5844565fb2
commit f2561fdeec
No known key found for this signature in database
1 changed files with 24 additions and 25 deletions

View File

@ -4,6 +4,7 @@ const {
FunctionPrototypeBind, FunctionPrototypeBind,
Promise, Promise,
PromiseReject, PromiseReject,
PromiseWithResolvers,
ReflectConstruct, ReflectConstruct,
SafePromisePrototypeFinally, SafePromisePrototypeFinally,
Symbol, Symbol,
@ -75,20 +76,19 @@ function setTimeout(after, value, options = kEmptyObject) {
} }
let oncancel; let oncancel;
const ret = new Promise((resolve, reject) => { const { promise, resolve, reject } = PromiseWithResolvers();
const timeout = new Timeout(resolve, after, [value], false, ref); const timeout = new Timeout(resolve, after, [value], false, ref);
insert(timeout, timeout._idleTimeout); insert(timeout, timeout._idleTimeout);
if (signal) { if (signal) {
oncancel = FunctionPrototypeBind(cancelListenerHandler, oncancel = FunctionPrototypeBind(cancelListenerHandler,
timeout, clearTimeout, reject, signal); timeout, clearTimeout, reject, signal);
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation; kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
signal.addEventListener('abort', oncancel, { __proto__: null, [kResistStopPropagation]: true }); signal.addEventListener('abort', oncancel, { __proto__: null, [kResistStopPropagation]: true });
} }
});
return oncancel !== undefined ? return oncancel !== undefined ?
SafePromisePrototypeFinally( SafePromisePrototypeFinally(
ret, promise,
() => signal.removeEventListener('abort', oncancel)) : ret; () => signal.removeEventListener('abort', oncancel)) : promise;
} }
function setImmediate(value, options = kEmptyObject) { function setImmediate(value, options = kEmptyObject) {
@ -113,21 +113,20 @@ function setImmediate(value, options = kEmptyObject) {
} }
let oncancel; let oncancel;
const ret = new Promise((resolve, reject) => { const { promise, resolve, reject } = PromiseWithResolvers();
const immediate = new Immediate(resolve, [value]); const immediate = new Immediate(resolve, [value]);
if (!ref) immediate.unref(); if (!ref) immediate.unref();
if (signal) { if (signal) {
oncancel = FunctionPrototypeBind(cancelListenerHandler, oncancel = FunctionPrototypeBind(cancelListenerHandler,
immediate, clearImmediate, reject, immediate, clearImmediate, reject,
signal); signal);
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation; kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
signal.addEventListener('abort', oncancel, { __proto__: null, [kResistStopPropagation]: true }); signal.addEventListener('abort', oncancel, { __proto__: null, [kResistStopPropagation]: true });
} }
});
return oncancel !== undefined ? return oncancel !== undefined ?
SafePromisePrototypeFinally( SafePromisePrototypeFinally(
ret, promise,
() => signal.removeEventListener('abort', oncancel)) : ret; () => signal.removeEventListener('abort', oncancel)) : promise;
} }
async function* setInterval(after, value, options = kEmptyObject) { async function* setInterval(after, value, options = kEmptyObject) {