Skip to content

Commit 148a64d

Browse files
authored
fix(async): allow numbers greater than Number.MAX_SAFE_INTEGER in deadline (#6810)
1 parent 80066bb commit 148a64d

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

async/deadline.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export interface DeadlineOptions {
2424
* custom `reason` before resolving or timing out.
2525
* @typeParam T The type of the provided and returned promise.
2626
* @param p The promise to make rejectable.
27-
* @param ms Duration in milliseconds for when the promise should time out.
27+
* @param ms Duration in milliseconds for when the promise should time out. If
28+
* greater than `Number.MAX_SAFE_INTEGER`, the deadline will never expire.
2829
* @param options Additional options.
2930
* @returns A promise that will reject if the provided duration runs out before resolving.
3031
*
@@ -43,7 +44,8 @@ export async function deadline<T>(
4344
ms: number,
4445
options: DeadlineOptions = {},
4546
): Promise<T> {
46-
const signals = [AbortSignal.timeout(ms)];
47+
const signals: AbortSignal[] = [];
48+
if (ms < Number.MAX_SAFE_INTEGER) signals.push(AbortSignal.timeout(ms));
4749
if (options.signal) signals.push(options.signal);
4850
return await abortable(p, AbortSignal.any(signals));
4951
}

async/deadline_test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,8 @@ Deno.test("deadline() handles already aborted signal", async () => {
9191
assertEquals(error.name, "AbortError");
9292
controller.abort();
9393
});
94+
95+
Deno.test("deadline() supports numbers greater than Number.MAX_SAFE_INTEGER", async () => {
96+
const promise = await deadline(Promise.resolve("Hello"), Infinity);
97+
assertEquals(promise, "Hello");
98+
});

0 commit comments

Comments
 (0)