Skip to content

Commit

Permalink
fix node:timers/promises types
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Jan 20, 2025
1 parent 5e538f6 commit 6bff32b
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions src/node/internal/internal_timers_promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ import {

const kScheduler = Symbol.for('kScheduler');

export async function setTimeout(
after: number | undefined,
value?: unknown,
export async function setTimeout<T = void>(
delay: number | undefined,
value?: T,
options: { signal?: AbortSignal; ref?: boolean } = {}
): Promise<typeof value> {
if (after !== undefined) {
validateNumber(after, 'delay');
): Promise<T> {
if (delay !== undefined) {
validateNumber(delay, 'delay');
}

validateObject(options, 'options');
Expand All @@ -61,11 +61,11 @@ export async function setTimeout(
throw new AbortError(undefined, { cause: signal.reason });
}

const { promise, resolve, reject } = Promise.withResolvers<typeof value>();
const { promise, resolve, reject } = Promise.withResolvers<T>();

const timer = timers.setTimeout(() => {
resolve(value);
}, after ?? 0);
resolve(value as T);
}, delay ?? 0);

if (signal) {
function onCancel(): void {
Expand All @@ -78,10 +78,10 @@ export async function setTimeout(
return promise;
}

export async function setImmediate(
value?: unknown,
export async function setImmediate<T>(
value?: T,
options: { signal?: AbortSignal; ref?: boolean } = {}
): Promise<typeof value> {
): Promise<T> {
validateObject(options, 'options');

// Ref options is a no-op.
Expand All @@ -100,10 +100,10 @@ export async function setImmediate(
throw new AbortError(undefined, { cause: signal.reason });
}

const { promise, resolve, reject } = Promise.withResolvers<typeof value>();
const { promise, resolve, reject } = Promise.withResolvers<T>();

const timer = globalThis.setImmediate(() => {
resolve(value);
resolve(value as T);
});

if (signal) {
Expand All @@ -118,13 +118,13 @@ export async function setImmediate(
return promise;
}

export async function* setInterval(
after?: number,
value?: unknown,
export async function* setInterval<T = void>(
delay?: number,
value?: T,
options: { signal?: AbortSignal; ref?: boolean } = {}
): AsyncGenerator {
if (after !== undefined) {
validateNumber(after, 'delay');
): AsyncGenerator<T> {
if (delay !== undefined) {
validateNumber(delay, 'delay');
}

validateObject(options, 'options');
Expand Down Expand Up @@ -155,7 +155,7 @@ export async function* setInterval(
callback?.();
callback = undefined;
},
after,
delay,
undefined,
true,
ref
Expand All @@ -179,7 +179,7 @@ export async function* setInterval(
await new Promise((resolve) => (callback = resolve));
}
for (; notYielded > 0; notYielded--) {
yield value;
yield value as T;
}
}
throw new AbortError(undefined, { cause: signal.reason });
Expand Down Expand Up @@ -214,7 +214,6 @@ class Scheduler {

public yield(): Promise<void> {
if (!this[kScheduler]) throw new ERR_INVALID_THIS('Scheduler');
// @ts-expect-error TS2555 Following Node.js implementation
return setImmediate();
}

Expand Down

0 comments on commit 6bff32b

Please sign in to comment.