Skip to content

Commit

Permalink
Ensure milliseconds argument is not NaN (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Oct 23, 2021
1 parent 7dd3117 commit 6b6c4e8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -8,7 +8,7 @@ export class TimeoutError extends Error {
export default function pTimeout(promise, milliseconds, fallback, options) {
let timer;
const cancelablePromise = new Promise((resolve, reject) => {
if (typeof milliseconds !== 'number' || milliseconds < 0) {
if (typeof milliseconds !== 'number' || Math.sign(milliseconds) !== 1) {
throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``);
}

Expand Down
4 changes: 4 additions & 0 deletions test.js
Expand Up @@ -20,6 +20,10 @@ test('throws when milliseconds is negative number', async t => {
await t.throwsAsync(pTimeout(delay(50), -1), {instanceOf: TypeError});
});

test('throws when milliseconds is NaN', async t => {
await t.throwsAsync(pTimeout(delay(50), Number.NaN), {instanceOf: TypeError});
});

test('handles milliseconds being `Infinity`', async t => {
t.is(
await pTimeout(delay(50, {value: fixture}), Number.POSITIVE_INFINITY),
Expand Down

0 comments on commit 6b6c4e8

Please sign in to comment.