Meta tweaks

This commit is contained in:
Sindre Sorhus 2019-03-12 15:21:47 +07:00
parent 9a429bc248
commit 0333387584
4 changed files with 20 additions and 19 deletions

12
index.d.ts vendored
View File

@ -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<ValueType>(
input: PromiseLike<ValueType>,
ms: number,
milliseconds: number,
message?: string | Error
): Promise<ValueType>;
@ -20,9 +20,9 @@ export default function pTimeout<ValueType>(
* 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<ValueType>(
*/
export default function pTimeout<ValueType, ReturnType>(
input: PromiseLike<ValueType>,
ms: number,
milliseconds: number,
fallback: () => ReturnType | Promise<ReturnType>
): Promise<ValueType | ReturnType>;

View File

@ -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),

View File

@ -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`

10
test.js
View File

@ -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);