Meta tweaks
This commit is contained in:
parent
9a429bc248
commit
0333387584
|
@ -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.
|
* 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 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'`.
|
* @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>(
|
export default function pTimeout<ValueType>(
|
||||||
input: PromiseLike<ValueType>,
|
input: PromiseLike<ValueType>,
|
||||||
ms: number,
|
milliseconds: number,
|
||||||
message?: string | Error
|
message?: string | Error
|
||||||
): Promise<ValueType>;
|
): 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.
|
* 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 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.
|
* @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
|
* @example
|
||||||
*
|
*
|
||||||
|
@ -37,7 +37,7 @@ export default function pTimeout<ValueType>(
|
||||||
*/
|
*/
|
||||||
export default function pTimeout<ValueType, ReturnType>(
|
export default function pTimeout<ValueType, ReturnType>(
|
||||||
input: PromiseLike<ValueType>,
|
input: PromiseLike<ValueType>,
|
||||||
ms: number,
|
milliseconds: number,
|
||||||
fallback: () => ReturnType | Promise<ReturnType>
|
fallback: () => ReturnType | Promise<ReturnType>
|
||||||
): Promise<ValueType | ReturnType>;
|
): Promise<ValueType | ReturnType>;
|
||||||
|
|
||||||
|
|
11
index.js
11
index.js
|
@ -9,9 +9,9 @@ class TimeoutError extends Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const pTimeout = (promise, ms, fallback) => new Promise((resolve, reject) => {
|
const pTimeout = (promise, milliseconds, fallback) => new Promise((resolve, reject) => {
|
||||||
if (typeof ms !== 'number' || ms < 0) {
|
if (typeof milliseconds !== 'number' || milliseconds < 0) {
|
||||||
throw new TypeError('Expected `ms` to be a positive number');
|
throw new TypeError('Expected `milliseconds` to be a positive number');
|
||||||
}
|
}
|
||||||
|
|
||||||
const timer = setTimeout(() => {
|
const timer = setTimeout(() => {
|
||||||
|
@ -25,7 +25,7 @@ const pTimeout = (promise, ms, fallback) => new Promise((resolve, reject) => {
|
||||||
return;
|
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);
|
const timeoutError = fallback instanceof Error ? fallback : new TimeoutError(message);
|
||||||
|
|
||||||
if (typeof promise.cancel === 'function') {
|
if (typeof promise.cancel === 'function') {
|
||||||
|
@ -33,8 +33,9 @@ const pTimeout = (promise, ms, fallback) => new Promise((resolve, reject) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
reject(timeoutError);
|
reject(timeoutError);
|
||||||
}, ms);
|
}, milliseconds);
|
||||||
|
|
||||||
|
// TODO: Use native `finally` keyword when targeting Node.js 10
|
||||||
pFinally(
|
pFinally(
|
||||||
// eslint-disable-next-line promise/prefer-await-to-then
|
// eslint-disable-next-line promise/prefer-await-to-then
|
||||||
promise.then(resolve, reject),
|
promise.then(resolve, reject),
|
||||||
|
|
|
@ -25,9 +25,9 @@ pTimeout(delayedPromise, 50).then(() => 'foo');
|
||||||
|
|
||||||
## API
|
## 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.
|
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.
|
Promise to decorate.
|
||||||
|
|
||||||
#### ms
|
#### milliseconds
|
||||||
|
|
||||||
Type: `number`
|
Type: `number`
|
||||||
|
|
||||||
|
|
10
test.js
10
test.js
|
@ -4,17 +4,17 @@ import PCancelable from 'p-cancelable';
|
||||||
import pTimeout from '.';
|
import pTimeout from '.';
|
||||||
|
|
||||||
const fixture = Symbol('fixture');
|
const fixture = Symbol('fixture');
|
||||||
const fixtureErr = new Error('fixture');
|
const fixtureError = new Error('fixture');
|
||||||
|
|
||||||
test('resolves before timeout', async t => {
|
test('resolves before timeout', async t => {
|
||||||
t.is(await pTimeout(delay(50).then(() => fixture), 200), fixture);
|
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);
|
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);
|
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 => {
|
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 => {
|
test('fallback argument', async t => {
|
||||||
await t.throwsAsync(pTimeout(delay(200), 50, 'rainbow'), 'rainbow');
|
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, 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, () => {
|
await t.throwsAsync(pTimeout(delay(200), 50, () => {
|
||||||
throw new RangeError('cake');
|
throw new RangeError('cake');
|
||||||
}), RangeError);
|
}), RangeError);
|
||||||
|
|
Loading…
Reference in New Issue