Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: use Promise.withResolvers() in timers #55720

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions lib/timers/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
FunctionPrototypeBind,
Promise,
PromiseReject,
PromiseWithResolvers,
ReflectConstruct,
SafePromisePrototypeFinally,
Symbol,
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Loading