diff --git a/index.d.ts b/index.d.ts index e0971a1..4731ff1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -4,13 +4,13 @@ * If you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out. * * @param input - Promise to decorate. - * @param ms - Milliseconds before timing out. + * @param milliseconds - Milliseconds before timing out. * @param message - Specify a custom error message or error. If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`. Default: `'Promise timed out after 50 milliseconds'`. - * @returns A decorated `input` that times out after `ms` time. + * @returns A decorated `input` that times out after `milliseconds` time. */ export default function pTimeout( input: PromiseLike, - ms: number, + milliseconds: number, message?: string | Error ): Promise; @@ -20,9 +20,9 @@ export default function pTimeout( * If you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out. * * @param input - Promise to decorate. - * @param ms - Milliseconds before timing out. + * @param milliseconds - Milliseconds before timing out. * @param fallback - Do something other than rejecting with an error on timeout. You could for example retry. - * @returns A decorated `input` that times out after `ms` time. + * @returns A decorated `input` that times out after `milliseconds` time. * * @example * @@ -37,7 +37,7 @@ export default function pTimeout( */ export default function pTimeout( input: PromiseLike, - ms: number, + milliseconds: number, fallback: () => ReturnType | Promise ): Promise; diff --git a/index.js b/index.js index 7e5a678..51f5732 100644 --- a/index.js +++ b/index.js @@ -9,9 +9,9 @@ class TimeoutError extends Error { } } -const pTimeout = (promise, ms, fallback) => new Promise((resolve, reject) => { - if (typeof ms !== 'number' || ms < 0) { - throw new TypeError('Expected `ms` to be a positive number'); +const pTimeout = (promise, milliseconds, fallback) => new Promise((resolve, reject) => { + if (typeof milliseconds !== 'number' || milliseconds < 0) { + throw new TypeError('Expected `milliseconds` to be a positive number'); } const timer = setTimeout(() => { @@ -25,7 +25,7 @@ const pTimeout = (promise, ms, fallback) => new Promise((resolve, reject) => { return; } - const message = typeof fallback === 'string' ? fallback : `Promise timed out after ${ms} milliseconds`; + const message = typeof fallback === 'string' ? fallback : `Promise timed out after ${milliseconds} milliseconds`; const timeoutError = fallback instanceof Error ? fallback : new TimeoutError(message); if (typeof promise.cancel === 'function') { @@ -33,8 +33,9 @@ const pTimeout = (promise, ms, fallback) => new Promise((resolve, reject) => { } reject(timeoutError); - }, ms); + }, milliseconds); + // TODO: Use native `finally` keyword when targeting Node.js 10 pFinally( // eslint-disable-next-line promise/prefer-await-to-then promise.then(resolve, reject), diff --git a/readme.md b/readme.md index 94ff3e3..8aec5ab 100644 --- a/readme.md +++ b/readme.md @@ -25,9 +25,9 @@ pTimeout(delayedPromise, 50).then(() => 'foo'); ## API -### pTimeout(input, ms, [message | fallback]) +### pTimeout(input, milliseconds, [message | fallback]) -Returns a decorated `input` that times out after `ms` time. +Returns a decorated `input` that times out after `milliseconds` time. If you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out. @@ -37,7 +37,7 @@ Type: `Promise` Promise to decorate. -#### ms +#### milliseconds Type: `number` diff --git a/test.js b/test.js index fe7bdc1..0ab8089 100644 --- a/test.js +++ b/test.js @@ -4,17 +4,17 @@ import PCancelable from 'p-cancelable'; import pTimeout from '.'; const fixture = Symbol('fixture'); -const fixtureErr = new Error('fixture'); +const fixtureError = new Error('fixture'); test('resolves before timeout', async t => { t.is(await pTimeout(delay(50).then(() => fixture), 200), fixture); }); -test('throws when ms is not number', async t => { +test('throws when milliseconds is not number', async t => { await t.throwsAsync(pTimeout(delay(50), '200'), TypeError); }); -test('throws when ms is negative number', async t => { +test('throws when milliseconds is negative number', async t => { await t.throwsAsync(pTimeout(delay(50), -1), TypeError); }); @@ -23,13 +23,13 @@ test('rejects after timeout', async t => { }); test('rejects before timeout if specified promise rejects', async t => { - await t.throwsAsync(pTimeout(delay(50).then(() => Promise.reject(fixtureErr)), 200), fixtureErr.message); + await t.throwsAsync(pTimeout(delay(50).then(() => Promise.reject(fixtureError)), 200), fixtureError.message); }); test('fallback argument', async t => { await t.throwsAsync(pTimeout(delay(200), 50, 'rainbow'), 'rainbow'); await t.throwsAsync(pTimeout(delay(200), 50, new RangeError('cake')), RangeError); - await t.throwsAsync(pTimeout(delay(200), 50, () => Promise.reject(fixtureErr)), fixtureErr.message); + await t.throwsAsync(pTimeout(delay(200), 50, () => Promise.reject(fixtureError)), fixtureError.message); await t.throwsAsync(pTimeout(delay(200), 50, () => { throw new RangeError('cake'); }), RangeError);