Skip to content

Commit

Permalink
Fix infinite milliseconds (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronami committed Jun 12, 2023
1 parent e8c2c9e commit 9260b09
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
10 changes: 5 additions & 5 deletions index.js
Expand Up @@ -50,11 +50,6 @@ export default function pTimeout(promise, options) {
throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``);
}

if (milliseconds === Number.POSITIVE_INFINITY) {
resolve(promise);
return;
}

if (options.signal) {
const {signal} = options;
if (signal.aborted) {
Expand All @@ -66,6 +61,11 @@ export default function pTimeout(promise, options) {
});
}

if (milliseconds === Number.POSITIVE_INFINITY) {
promise.then(resolve, reject);
return;
}

// We create the error outside of `setTimeout` to preserve the stack trace.
const timeoutError = new TimeoutError();

Expand Down
13 changes: 13 additions & 0 deletions test.js
Expand Up @@ -130,4 +130,17 @@ if (globalThis.AbortController !== undefined) {
name: 'AbortError',
});
});

test('aborts even if milliseconds are set to infinity', async t => {
const abortController = new AbortController();

abortController.abort();

await t.throwsAsync(pTimeout(delay(3000), {
milliseconds: Number.POSITIVE_INFINITY,
signal: abortController.signal,
}), {
name: 'AbortError',
});
});
}

0 comments on commit 9260b09

Please sign in to comment.