@common.js/p-timeout 6.0.0 → 6.1.1
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 +1 -1
- package/index.d.ts +20 -4
- package/index.js +10 -3
- package/package.json +2 -2
- package/readme.md +0 -146
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.
|
|
5
|
+
Exported from [p-timeout@6.1.1](https://www.npmjs.com/package/p-timeout/v/6.1.1) 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
|
-
|
|
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.
|
|
@@ -124,11 +136,15 @@ const delayedPromise = () => setTimeout(200);
|
|
|
124
136
|
await pTimeout(delayedPromise(), {
|
|
125
137
|
milliseconds: 50,
|
|
126
138
|
fallback: () => {
|
|
127
|
-
return pTimeout(delayedPromise(), 300);
|
|
139
|
+
return pTimeout(delayedPromise(), {milliseconds: 300});
|
|
128
140
|
}
|
|
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
|
@@ -339,6 +339,8 @@ function pTimeout(promise, options) {
|
|
|
339
339
|
reject(getAbortedReason(signal));
|
|
340
340
|
});
|
|
341
341
|
}
|
|
342
|
+
// We create the error outside of `setTimeout` to preserve the stack trace.
|
|
343
|
+
var timeoutError = new TimeoutError();
|
|
342
344
|
timer = customTimers.setTimeout.call(undefined, function() {
|
|
343
345
|
if (fallback) {
|
|
344
346
|
try {
|
|
@@ -348,12 +350,17 @@ function pTimeout(promise, options) {
|
|
|
348
350
|
}
|
|
349
351
|
return;
|
|
350
352
|
}
|
|
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
353
|
if (typeof promise.cancel === "function") {
|
|
354
354
|
promise.cancel();
|
|
355
355
|
}
|
|
356
|
-
|
|
356
|
+
if (message === false) {
|
|
357
|
+
resolve();
|
|
358
|
+
} else if (_instanceof(message, Error)) {
|
|
359
|
+
reject(message);
|
|
360
|
+
} else {
|
|
361
|
+
timeoutError.message = message !== null && message !== void 0 ? message : "Promise timed out after ".concat(milliseconds, " milliseconds");
|
|
362
|
+
reject(timeoutError);
|
|
363
|
+
}
|
|
357
364
|
}, milliseconds);
|
|
358
365
|
_asyncToGenerator(function() {
|
|
359
366
|
var error;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@common.js/p-timeout",
|
|
3
|
-
"version": "6.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "6.1.1",
|
|
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)
|