mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
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>
This commit is contained in:
parent
e0145b4c38
commit
f38cefa04b
@ -4,6 +4,7 @@ const {
|
||||
FunctionPrototypeBind,
|
||||
Promise,
|
||||
PromiseReject,
|
||||
PromiseWithResolvers,
|
||||
ReflectConstruct,
|
||||
SafePromisePrototypeFinally,
|
||||
Symbol,
|
||||
@ -75,20 +76,19 @@ function setTimeout(after, value, options = kEmptyObject) {
|
||||
}
|
||||
|
||||
let oncancel;
|
||||
const ret = new Promise((resolve, reject) => {
|
||||
const timeout = new Timeout(resolve, after, [value], false, ref);
|
||||
insert(timeout, timeout._idleTimeout);
|
||||
if (signal) {
|
||||
oncancel = FunctionPrototypeBind(cancelListenerHandler,
|
||||
timeout, clearTimeout, reject, signal);
|
||||
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
|
||||
signal.addEventListener('abort', oncancel, { __proto__: null, [kResistStopPropagation]: true });
|
||||
}
|
||||
});
|
||||
const { promise, resolve, reject } = PromiseWithResolvers();
|
||||
const timeout = new Timeout(resolve, after, [value], false, ref);
|
||||
insert(timeout, timeout._idleTimeout);
|
||||
if (signal) {
|
||||
oncancel = FunctionPrototypeBind(cancelListenerHandler,
|
||||
timeout, clearTimeout, reject, signal);
|
||||
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
|
||||
signal.addEventListener('abort', oncancel, { __proto__: null, [kResistStopPropagation]: true });
|
||||
}
|
||||
return oncancel !== undefined ?
|
||||
SafePromisePrototypeFinally(
|
||||
ret,
|
||||
() => signal.removeEventListener('abort', oncancel)) : ret;
|
||||
promise,
|
||||
() => signal.removeEventListener('abort', oncancel)) : promise;
|
||||
}
|
||||
|
||||
function setImmediate(value, options = kEmptyObject) {
|
||||
@ -113,21 +113,20 @@ function setImmediate(value, options = kEmptyObject) {
|
||||
}
|
||||
|
||||
let oncancel;
|
||||
const ret = new Promise((resolve, reject) => {
|
||||
const immediate = new Immediate(resolve, [value]);
|
||||
if (!ref) immediate.unref();
|
||||
if (signal) {
|
||||
oncancel = FunctionPrototypeBind(cancelListenerHandler,
|
||||
immediate, clearImmediate, reject,
|
||||
signal);
|
||||
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
|
||||
signal.addEventListener('abort', oncancel, { __proto__: null, [kResistStopPropagation]: true });
|
||||
}
|
||||
});
|
||||
const { promise, resolve, reject } = PromiseWithResolvers();
|
||||
const immediate = new Immediate(resolve, [value]);
|
||||
if (!ref) immediate.unref();
|
||||
if (signal) {
|
||||
oncancel = FunctionPrototypeBind(cancelListenerHandler,
|
||||
immediate, clearImmediate, reject,
|
||||
signal);
|
||||
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
|
||||
signal.addEventListener('abort', oncancel, { __proto__: null, [kResistStopPropagation]: true });
|
||||
}
|
||||
return oncancel !== undefined ?
|
||||
SafePromisePrototypeFinally(
|
||||
ret,
|
||||
() => signal.removeEventListener('abort', oncancel)) : ret;
|
||||
promise,
|
||||
() => signal.removeEventListener('abort', oncancel)) : promise;
|
||||
}
|
||||
|
||||
async function* setInterval(after, value, options = kEmptyObject) {
|
||||
|
Loading…
Reference in New Issue
Block a user