diff --git a/index.d.ts b/index.d.ts index 375d7b0..211275e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -43,11 +43,23 @@ export type Options = { fallback?: () => ReturnType | Promise; /** - Specify a custom error message or error. + Specify a custom error message or error to throw when it times out: - If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`. + - `message: 'too slow'` will throw `TimeoutError('too slow')` + - `message: new MyCustomError('it’s over 9000')` will throw the same error instance + - `message: false` will make the promise resolve with `undefined` instead of rejecting + + If you do a custom error, it's recommended to sub-class `TimeoutError`: + + ``` + import {TimeoutError} from 'p-timeout'; + + class MyCustomError extends TimeoutError { + name = "MyCustomError"; + } + ``` */ - message?: string | Error; + message?: string | Error | false; /** Custom implementations for the `setTimeout` and `clearTimeout` functions. @@ -129,6 +141,10 @@ await pTimeout(delayedPromise(), { }); ``` */ +export default function pTimeout( + input: PromiseLike, + options: Options & {message: false} +): ClearablePromise; export default function pTimeout( input: PromiseLike, options: Options diff --git a/index.js b/index.js index ebd6c2d..220ccb6 100644 --- a/index.js +++ b/index.js @@ -77,14 +77,18 @@ export default function pTimeout(promise, options) { return; } - const errorMessage = typeof message === 'string' ? message : `Promise timed out after ${milliseconds} milliseconds`; - const timeoutError = message instanceof Error ? message : new TimeoutError(errorMessage); - if (typeof promise.cancel === 'function') { promise.cancel(); } - reject(timeoutError); + if (message === false) { + resolve(); + } else if (message instanceof Error) { + reject(message); + } else { + const errorMessage = message ?? `Promise timed out after ${milliseconds} milliseconds`; + reject(new TimeoutError(errorMessage)); + } }, milliseconds); (async () => { diff --git a/index.test-d.ts b/index.test-d.ts index 3a54965..13f8bc6 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -16,6 +16,9 @@ pTimeout(delayedPromise(), {milliseconds: 50}).then(value => { pTimeout(delayedPromise(), {milliseconds: 50, message: 'error'}).then(value => { expectType(value); }); +pTimeout(delayedPromise(), {milliseconds: 50, message: false}).then(value => { + expectType(value); +}); pTimeout(delayedPromise(), {milliseconds: 50, message: new Error('error')}).then(value => { expectType(value); }); diff --git a/readme.md b/readme.md index 12c7c52..26e2004 100644 --- a/readme.md +++ b/readme.md @@ -50,12 +50,24 @@ Passing `Infinity` will cause it to never time out. ##### message -Type: `string | Error`\ +Type: `string | Error | false`\ Default: `'Promise timed out after 50 milliseconds'` -Specify a custom error message or error. +Specify a custom error message or error to throw when it times out: -If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`. +- `message: 'too slow'` will throw `TimeoutError('too slow')` +- `message: new MyCustomError('it’s over 9000')` will throw the same error instance +- `message: false` will make the promise resolve with `undefined` instead of rejecting + +If you do a custom error, it's recommended to sub-class `TimeoutError`: + +```js +import {TimeoutError} from 'p-timeout'; + +class MyCustomError extends TimeoutError { + name = "MyCustomError"; +} +``` ##### fallback diff --git a/test.js b/test.js index 00088c9..6375f63 100644 --- a/test.js +++ b/test.js @@ -35,6 +35,13 @@ test('rejects after timeout', async t => { await t.throwsAsync(pTimeout(delay(200), {milliseconds: 50}), {instanceOf: TimeoutError}); }); +test('resolves after timeout with message:false', async t => { + t.is( + await pTimeout(delay(200), {milliseconds: 50, message: false}), + undefined, + ); +}); + test('rejects before timeout if specified promise rejects', async t => { await t.throwsAsync(pTimeout(delay(50).then(() => { throw fixtureError;