Add types, tests, docs

This commit is contained in:
Federico Brigante 2022-11-25 16:54:50 +08:00
parent 557dbb019a
commit 4bf9e3b678
4 changed files with 11 additions and 4 deletions

2
index.d.ts vendored
View File

@ -47,7 +47,7 @@ export type Options<ReturnType> = {
If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`. If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`.
*/ */
message?: string | Error; message?: string | Error | false;
/** /**
Custom implementations for the `setTimeout` and `clearTimeout` functions. Custom implementations for the `setTimeout` and `clearTimeout` functions.

View File

@ -80,7 +80,7 @@ export default function pTimeout(promise, options) {
if (typeof promise.cancel === 'function') { if (typeof promise.cancel === 'function') {
promise.cancel(); promise.cancel();
} }
if (message === false) { if (message === false) {
resolve(); resolve();
} }

View File

@ -50,10 +50,10 @@ Passing `Infinity` will cause it to never time out.
##### message ##### message
Type: `string | Error`\ Type: `string | Error | false`\
Default: `'Promise timed out after 50 milliseconds'` Default: `'Promise timed out after 50 milliseconds'`
Specify a custom error message or error. Specify a custom error message or error. If you supply `false`, the promise will resolve successfully instead of rejecting.
If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`. If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`.

View File

@ -35,6 +35,13 @@ test('rejects after timeout', async t => {
await t.throwsAsync(pTimeout(delay(200), {milliseconds: 50}), {instanceOf: TimeoutError}); 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 => { test('rejects before timeout if specified promise rejects', async t => {
await t.throwsAsync(pTimeout(delay(50).then(() => { await t.throwsAsync(pTimeout(delay(50).then(() => {
throw fixtureError; throw fixtureError;