From 6b6c4e85c34c5fab21bf3d8fc06ddee7e0dfa88d Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Sat, 23 Oct 2021 15:54:29 +0200 Subject: [PATCH] Ensure milliseconds argument is not NaN (#25) --- index.js | 2 +- test.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ddb8b1c..629b868 100644 --- a/index.js +++ b/index.js @@ -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}\``); } diff --git a/test.js b/test.js index ad86baf..c4d172b 100644 --- a/test.js +++ b/test.js @@ -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),