Ensure milliseconds argument is not NaN (#25)
This commit is contained in:
parent
7dd311757e
commit
6b6c4e85c3
2
index.js
2
index.js
|
@ -8,7 +8,7 @@ export class TimeoutError extends Error {
|
||||||
export default function pTimeout(promise, milliseconds, fallback, options) {
|
export default function pTimeout(promise, milliseconds, fallback, options) {
|
||||||
let timer;
|
let timer;
|
||||||
const cancelablePromise = new Promise((resolve, reject) => {
|
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}\``);
|
throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
4
test.js
4
test.js
|
@ -20,6 +20,10 @@ test('throws when milliseconds is negative number', async t => {
|
||||||
await t.throwsAsync(pTimeout(delay(50), -1), {instanceOf: TypeError});
|
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 => {
|
test('handles milliseconds being `Infinity`', async t => {
|
||||||
t.is(
|
t.is(
|
||||||
await pTimeout(delay(50, {value: fixture}), Number.POSITIVE_INFINITY),
|
await pTimeout(delay(50, {value: fixture}), Number.POSITIVE_INFINITY),
|
||||||
|
|
Loading…
Reference in New Issue