From 4bf9e3b67879516d910225bc6ef4540ce22a4d80 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Fri, 25 Nov 2022 16:54:50 +0800 Subject: [PATCH] Add types, tests, docs --- index.d.ts | 2 +- index.js | 2 +- readme.md | 4 ++-- test.js | 7 +++++++ 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 375d7b0..83df723 100644 --- a/index.d.ts +++ b/index.d.ts @@ -47,7 +47,7 @@ export type Options = { 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. diff --git a/index.js b/index.js index 7b3d79d..86ac857 100644 --- a/index.js +++ b/index.js @@ -80,7 +80,7 @@ export default function pTimeout(promise, options) { if (typeof promise.cancel === 'function') { promise.cancel(); } - + if (message === false) { resolve(); } diff --git a/readme.md b/readme.md index 12c7c52..e17d864 100644 --- a/readme.md +++ b/readme.md @@ -50,10 +50,10 @@ 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. 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`. 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;