@common.js/p-timeout 6.0.0 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  The [p-timeout](https://www.npmjs.com/package/p-timeout) package exported as CommonJS modules.
4
4
 
5
- Exported from [p-timeout@6.0.0](https://www.npmjs.com/package/p-timeout/v/6.0.0) using https://github.com/etienne-martin/common.js.
5
+ Exported from [p-timeout@6.1.0](https://www.npmjs.com/package/p-timeout/v/6.1.0) using https://github.com/etienne-martin/common.js.
package/index.d.ts CHANGED
@@ -43,11 +43,23 @@ export type Options<ReturnType> = {
43
43
  fallback?: () => ReturnType | Promise<ReturnType>;
44
44
 
45
45
  /**
46
- Specify a custom error message or error.
46
+ Specify a custom error message or error to throw when it times out:
47
47
 
48
- If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`.
48
+ - `message: 'too slow'` will throw `TimeoutError('too slow')`
49
+ - `message: new MyCustomError('it’s over 9000')` will throw the same error instance
50
+ - `message: false` will make the promise resolve with `undefined` instead of rejecting
51
+
52
+ If you do a custom error, it's recommended to sub-class `TimeoutError`:
53
+
54
+ ```
55
+ import {TimeoutError} from 'p-timeout';
56
+
57
+ class MyCustomError extends TimeoutError {
58
+ name = "MyCustomError";
59
+ }
60
+ ```
49
61
  */
50
- message?: string | Error;
62
+ message?: string | Error | false;
51
63
 
52
64
  /**
53
65
  Custom implementations for the `setTimeout` and `clearTimeout` functions.
@@ -129,6 +141,10 @@ await pTimeout(delayedPromise(), {
129
141
  });
130
142
  ```
131
143
  */
144
+ export default function pTimeout<ValueType, ReturnType = ValueType>(
145
+ input: PromiseLike<ValueType>,
146
+ options: Options<ReturnType> & {message: false}
147
+ ): ClearablePromise<ValueType | ReturnType | undefined>;
132
148
  export default function pTimeout<ValueType, ReturnType = ValueType>(
133
149
  input: PromiseLike<ValueType>,
134
150
  options: Options<ReturnType>
package/index.js CHANGED
@@ -348,12 +348,17 @@ function pTimeout(promise, options) {
348
348
  }
349
349
  return;
350
350
  }
351
- var errorMessage = typeof message === "string" ? message : "Promise timed out after ".concat(milliseconds, " milliseconds");
352
- var timeoutError = _instanceof(message, Error) ? message : new TimeoutError(errorMessage);
353
351
  if (typeof promise.cancel === "function") {
354
352
  promise.cancel();
355
353
  }
356
- reject(timeoutError);
354
+ if (message === false) {
355
+ resolve();
356
+ } else if (_instanceof(message, Error)) {
357
+ reject(message);
358
+ } else {
359
+ var errorMessage = message !== null && message !== void 0 ? message : "Promise timed out after ".concat(milliseconds, " milliseconds");
360
+ reject(new TimeoutError(errorMessage));
361
+ }
357
362
  }, milliseconds);
358
363
  _asyncToGenerator(function() {
359
364
  var error;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@common.js/p-timeout",
3
- "version": "6.0.0",
4
- "description": "Timeout a promise after a specified amount of time",
3
+ "version": "6.1.0",
4
+ "description": "p-timeout package exported as CommonJS modules",
5
5
  "license": "MIT",
6
6
  "repository": "etienne-martin/common.js",
7
7
  "funding": "https://github.com/sponsors/sindresorhus",
package/readme.md DELETED
@@ -1,146 +0,0 @@
1
- # p-timeout
2
-
3
- > Timeout a promise after a specified amount of time
4
-
5
- ## Install
6
-
7
- ```sh
8
- npm install p-timeout
9
- ```
10
-
11
- ## Usage
12
-
13
- ```js
14
- import {setTimeout} from 'node:timers/promises';
15
- import pTimeout from 'p-timeout';
16
-
17
- const delayedPromise = setTimeout(200);
18
-
19
- await pTimeout(delayedPromise, {
20
- milliseconds: 50,
21
- });
22
- //=> [TimeoutError: Promise timed out after 50 milliseconds]
23
- ```
24
-
25
- ## API
26
-
27
- ### pTimeout(input, options)
28
-
29
- Returns a decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout.
30
-
31
- 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.
32
-
33
- #### input
34
-
35
- Type: `Promise`
36
-
37
- Promise to decorate.
38
-
39
- #### options
40
-
41
- Type: `object`
42
-
43
- ##### milliseconds
44
-
45
- Type: `number`
46
-
47
- Milliseconds before timing out.
48
-
49
- Passing `Infinity` will cause it to never time out.
50
-
51
- ##### message
52
-
53
- Type: `string | Error`\
54
- Default: `'Promise timed out after 50 milliseconds'`
55
-
56
- Specify a custom error message or error.
57
-
58
- If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`.
59
-
60
- ##### fallback
61
-
62
- Type: `Function`
63
-
64
- Do something other than rejecting with an error on timeout.
65
-
66
- You could for example retry:
67
-
68
- ```js
69
- import {setTimeout} from 'node:timers/promises';
70
- import pTimeout from 'p-timeout';
71
-
72
- const delayedPromise = () => setTimeout(200);
73
-
74
- await pTimeout(delayedPromise(), {
75
- milliseconds: 50,
76
- fallback: () => {
77
- return pTimeout(delayedPromise(), 300);
78
- },
79
- });
80
- ```
81
-
82
- ##### customTimers
83
-
84
- Type: `object` with function properties `setTimeout` and `clearTimeout`
85
-
86
- Custom implementations for the `setTimeout` and `clearTimeout` functions.
87
-
88
- Useful for testing purposes, in particular to work around [`sinon.useFakeTimers()`](https://sinonjs.org/releases/latest/fake-timers/).
89
-
90
- Example:
91
-
92
- ```js
93
- import {setTimeout} from 'node:timers/promises';
94
- import pTimeout from 'p-timeout';
95
-
96
- const originalSetTimeout = setTimeout;
97
- const originalClearTimeout = clearTimeout;
98
-
99
- sinon.useFakeTimers();
100
-
101
- // Use `pTimeout` without being affected by `sinon.useFakeTimers()`:
102
- await pTimeout(doSomething(), {
103
- milliseconds: 2000,
104
- customTimers: {
105
- setTimeout: originalSetTimeout,
106
- clearTimeout: originalClearTimeout
107
- }
108
- });
109
- ```
110
-
111
- #### signal
112
-
113
- Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
114
-
115
- You can abort the promise using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
116
-
117
- *Requires Node.js 16 or later.*
118
-
119
- ```js
120
- import pTimeout from 'p-timeout';
121
- import delay from 'delay';
122
-
123
- const delayedPromise = delay(3000);
124
-
125
- const abortController = new AbortController();
126
-
127
- setTimeout(() => {
128
- abortController.abort();
129
- }, 100);
130
-
131
- await pTimeout(delayedPromise, {
132
- milliseconds: 2000,
133
- signal: abortController.signal
134
- });
135
- ```
136
-
137
- ### TimeoutError
138
-
139
- Exposed for instance checking and sub-classing.
140
-
141
- ## Related
142
-
143
- - [delay](https://github.com/sindresorhus/delay) - Delay a promise a specified amount of time
144
- - [p-min-delay](https://github.com/sindresorhus/p-min-delay) - Delay a promise a minimum amount of time
145
- - [p-retry](https://github.com/sindresorhus/p-retry) - Retry a promise-returning function
146
- - [More…](https://github.com/sindresorhus/promise-fun)