@common.js/p-timeout 5.1.0 → 6.0.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 +1 -1
- package/index.d.ts +54 -45
- package/index.js +11 -41
- package/package.json +8 -7
- package/readme.md +25 -19
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@
|
|
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.
|
package/index.d.ts
CHANGED
|
@@ -1,18 +1,54 @@
|
|
|
1
|
-
/* eslint-disable import/export */
|
|
2
|
-
|
|
3
1
|
export class TimeoutError extends Error {
|
|
4
2
|
readonly name: 'TimeoutError';
|
|
5
3
|
constructor(message?: string);
|
|
6
4
|
}
|
|
7
5
|
|
|
8
|
-
export interface ClearablePromise<T> extends Promise<T>{
|
|
6
|
+
export interface ClearablePromise<T> extends Promise<T> {
|
|
9
7
|
/**
|
|
10
8
|
Clear the timeout.
|
|
11
9
|
*/
|
|
12
10
|
clear: () => void;
|
|
13
11
|
}
|
|
14
12
|
|
|
15
|
-
export type Options = {
|
|
13
|
+
export type Options<ReturnType> = {
|
|
14
|
+
/**
|
|
15
|
+
Milliseconds before timing out.
|
|
16
|
+
|
|
17
|
+
Passing `Infinity` will cause it to never time out.
|
|
18
|
+
*/
|
|
19
|
+
milliseconds: number;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
Do something other than rejecting with an error on timeout.
|
|
23
|
+
|
|
24
|
+
You could for example retry:
|
|
25
|
+
|
|
26
|
+
@example
|
|
27
|
+
```
|
|
28
|
+
import {setTimeout} from 'node:timers/promises';
|
|
29
|
+
import pTimeout from 'p-timeout';
|
|
30
|
+
|
|
31
|
+
const delayedPromise = () => setTimeout(200);
|
|
32
|
+
|
|
33
|
+
await pTimeout(delayedPromise(), {
|
|
34
|
+
milliseconds: 50,
|
|
35
|
+
fallback: () => {
|
|
36
|
+
return pTimeout(delayedPromise(), {
|
|
37
|
+
milliseconds: 300
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
*/
|
|
43
|
+
fallback?: () => ReturnType | Promise<ReturnType>;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
Specify a custom error message or error.
|
|
47
|
+
|
|
48
|
+
If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`.
|
|
49
|
+
*/
|
|
50
|
+
message?: string | Error;
|
|
51
|
+
|
|
16
52
|
/**
|
|
17
53
|
Custom implementations for the `setTimeout` and `clearTimeout` functions.
|
|
18
54
|
|
|
@@ -29,7 +65,8 @@ export type Options = {
|
|
|
29
65
|
sinon.useFakeTimers();
|
|
30
66
|
|
|
31
67
|
// Use `pTimeout` without being affected by `sinon.useFakeTimers()`:
|
|
32
|
-
await pTimeout(doSomething(),
|
|
68
|
+
await pTimeout(doSomething(), {
|
|
69
|
+
milliseconds: 2000,
|
|
33
70
|
customTimers: {
|
|
34
71
|
setTimeout: originalSetTimeout,
|
|
35
72
|
clearTimeout: originalClearTimeout
|
|
@@ -38,8 +75,8 @@ export type Options = {
|
|
|
38
75
|
```
|
|
39
76
|
*/
|
|
40
77
|
readonly customTimers?: {
|
|
41
|
-
setTimeout: typeof
|
|
42
|
-
clearTimeout: typeof
|
|
78
|
+
setTimeout: typeof globalThis.setTimeout;
|
|
79
|
+
clearTimeout: typeof globalThis.clearTimeout;
|
|
43
80
|
};
|
|
44
81
|
|
|
45
82
|
/**
|
|
@@ -60,7 +97,8 @@ export type Options = {
|
|
|
60
97
|
abortController.abort();
|
|
61
98
|
}, 100);
|
|
62
99
|
|
|
63
|
-
await pTimeout(delayedPromise,
|
|
100
|
+
await pTimeout(delayedPromise, {
|
|
101
|
+
milliseconds: 2000,
|
|
64
102
|
signal: abortController.signal
|
|
65
103
|
});
|
|
66
104
|
```
|
|
@@ -74,53 +112,24 @@ Timeout a promise after a specified amount of time.
|
|
|
74
112
|
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.
|
|
75
113
|
|
|
76
114
|
@param input - Promise to decorate.
|
|
77
|
-
@param milliseconds - Milliseconds before timing out.
|
|
78
|
-
@param message - Specify a custom error message or error. If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`. Default: `'Promise timed out after 50 milliseconds'`.
|
|
79
|
-
@returns A decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout.
|
|
80
|
-
|
|
81
|
-
@example
|
|
82
|
-
```
|
|
83
|
-
import {setTimeout} from 'timers/promises';
|
|
84
|
-
import pTimeout from 'p-timeout';
|
|
85
|
-
|
|
86
|
-
const delayedPromise = setTimeout(200);
|
|
87
|
-
|
|
88
|
-
await pTimeout(delayedPromise, 50);
|
|
89
|
-
//=> [TimeoutError: Promise timed out after 50 milliseconds]
|
|
90
|
-
```
|
|
91
|
-
*/
|
|
92
|
-
export default function pTimeout<ValueType>(
|
|
93
|
-
input: PromiseLike<ValueType>,
|
|
94
|
-
milliseconds: number,
|
|
95
|
-
message?: string | Error,
|
|
96
|
-
options?: Options
|
|
97
|
-
): ClearablePromise<ValueType>;
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
Timeout a promise after a specified amount of time.
|
|
101
|
-
|
|
102
|
-
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.
|
|
103
|
-
|
|
104
|
-
@param input - Promise to decorate.
|
|
105
|
-
@param milliseconds - Milliseconds before timing out. Passing `Infinity` will cause it to never time out.
|
|
106
|
-
@param fallback - Do something other than rejecting with an error on timeout. You could for example retry.
|
|
107
115
|
@returns A decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout.
|
|
108
116
|
|
|
109
117
|
@example
|
|
110
118
|
```
|
|
111
|
-
import {setTimeout} from 'timers/promises';
|
|
119
|
+
import {setTimeout} from 'node:timers/promises';
|
|
112
120
|
import pTimeout from 'p-timeout';
|
|
113
121
|
|
|
114
122
|
const delayedPromise = () => setTimeout(200);
|
|
115
123
|
|
|
116
|
-
await pTimeout(delayedPromise(),
|
|
117
|
-
|
|
124
|
+
await pTimeout(delayedPromise(), {
|
|
125
|
+
milliseconds: 50,
|
|
126
|
+
fallback: () => {
|
|
127
|
+
return pTimeout(delayedPromise(), 300);
|
|
128
|
+
}
|
|
118
129
|
});
|
|
119
130
|
```
|
|
120
131
|
*/
|
|
121
|
-
export default function pTimeout<ValueType, ReturnType>(
|
|
132
|
+
export default function pTimeout<ValueType, ReturnType = ValueType>(
|
|
122
133
|
input: PromiseLike<ValueType>,
|
|
123
|
-
|
|
124
|
-
fallback: () => ReturnType | Promise<ReturnType>,
|
|
125
|
-
options?: Options
|
|
134
|
+
options: Options<ReturnType>
|
|
126
135
|
): ClearablePromise<ValueType | ReturnType>;
|
package/index.js
CHANGED
|
@@ -87,19 +87,6 @@ function _construct(Parent, args, Class) {
|
|
|
87
87
|
}
|
|
88
88
|
return _construct.apply(null, arguments);
|
|
89
89
|
}
|
|
90
|
-
function _defineProperty(obj, key, value) {
|
|
91
|
-
if (key in obj) {
|
|
92
|
-
Object.defineProperty(obj, key, {
|
|
93
|
-
value: value,
|
|
94
|
-
enumerable: true,
|
|
95
|
-
configurable: true,
|
|
96
|
-
writable: true
|
|
97
|
-
});
|
|
98
|
-
} else {
|
|
99
|
-
obj[key] = value;
|
|
100
|
-
}
|
|
101
|
-
return obj;
|
|
102
|
-
}
|
|
103
90
|
function _getPrototypeOf(o) {
|
|
104
91
|
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
|
105
92
|
return o.__proto__ || Object.getPrototypeOf(o);
|
|
@@ -129,21 +116,6 @@ function _instanceof(left, right) {
|
|
|
129
116
|
function _isNativeFunction(fn) {
|
|
130
117
|
return Function.toString.call(fn).indexOf("[native code]") !== -1;
|
|
131
118
|
}
|
|
132
|
-
function _objectSpread(target) {
|
|
133
|
-
for(var i = 1; i < arguments.length; i++){
|
|
134
|
-
var source = arguments[i] != null ? arguments[i] : {};
|
|
135
|
-
var ownKeys = Object.keys(source);
|
|
136
|
-
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
137
|
-
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
138
|
-
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
139
|
-
}));
|
|
140
|
-
}
|
|
141
|
-
ownKeys.forEach(function(key) {
|
|
142
|
-
_defineProperty(target, key, source[key]);
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
return target;
|
|
146
|
-
}
|
|
147
119
|
function _possibleConstructorReturn(self, call) {
|
|
148
120
|
if (call && (_typeof(call) === "object" || typeof call === "function")) {
|
|
149
121
|
return call;
|
|
@@ -344,7 +316,11 @@ TODO: Remove below function and just 'reject(signal.reason)' when targeting Node
|
|
|
344
316
|
var reason = signal.reason === undefined ? getDOMException("This operation was aborted.") : signal.reason;
|
|
345
317
|
return _instanceof(reason, Error) ? reason : getDOMException(reason);
|
|
346
318
|
};
|
|
347
|
-
function pTimeout(promise,
|
|
319
|
+
function pTimeout(promise, options) {
|
|
320
|
+
var milliseconds = options.milliseconds, fallback = options.fallback, message = options.message, _customTimers = options.customTimers, customTimers = _customTimers === void 0 ? {
|
|
321
|
+
setTimeout: setTimeout,
|
|
322
|
+
clearTimeout: clearTimeout
|
|
323
|
+
} : _customTimers;
|
|
348
324
|
var timer;
|
|
349
325
|
var cancelablePromise = new Promise(function(resolve, reject) {
|
|
350
326
|
if (typeof milliseconds !== "number" || Math.sign(milliseconds) !== 1) {
|
|
@@ -354,12 +330,6 @@ function pTimeout(promise, milliseconds, fallback, options) {
|
|
|
354
330
|
resolve(promise);
|
|
355
331
|
return;
|
|
356
332
|
}
|
|
357
|
-
options = _objectSpread({
|
|
358
|
-
customTimers: {
|
|
359
|
-
setTimeout: setTimeout,
|
|
360
|
-
clearTimeout: clearTimeout
|
|
361
|
-
}
|
|
362
|
-
}, options);
|
|
363
333
|
if (options.signal) {
|
|
364
334
|
var signal = options.signal;
|
|
365
335
|
if (signal.aborted) {
|
|
@@ -369,8 +339,8 @@ function pTimeout(promise, milliseconds, fallback, options) {
|
|
|
369
339
|
reject(getAbortedReason(signal));
|
|
370
340
|
});
|
|
371
341
|
}
|
|
372
|
-
timer =
|
|
373
|
-
if (
|
|
342
|
+
timer = customTimers.setTimeout.call(undefined, function() {
|
|
343
|
+
if (fallback) {
|
|
374
344
|
try {
|
|
375
345
|
resolve(fallback());
|
|
376
346
|
} catch (error) {
|
|
@@ -378,8 +348,8 @@ function pTimeout(promise, milliseconds, fallback, options) {
|
|
|
378
348
|
}
|
|
379
349
|
return;
|
|
380
350
|
}
|
|
381
|
-
var
|
|
382
|
-
var timeoutError = _instanceof(
|
|
351
|
+
var errorMessage = typeof message === "string" ? message : "Promise timed out after ".concat(milliseconds, " milliseconds");
|
|
352
|
+
var timeoutError = _instanceof(message, Error) ? message : new TimeoutError(errorMessage);
|
|
383
353
|
if (typeof promise.cancel === "function") {
|
|
384
354
|
promise.cancel();
|
|
385
355
|
}
|
|
@@ -416,7 +386,7 @@ function pTimeout(promise, milliseconds, fallback, options) {
|
|
|
416
386
|
4
|
|
417
387
|
];
|
|
418
388
|
case 3:
|
|
419
|
-
|
|
389
|
+
customTimers.clearTimeout.call(undefined, timer);
|
|
420
390
|
return [
|
|
421
391
|
7
|
|
422
392
|
];
|
|
@@ -429,7 +399,7 @@ function pTimeout(promise, milliseconds, fallback, options) {
|
|
|
429
399
|
})();
|
|
430
400
|
});
|
|
431
401
|
cancelablePromise.clear = function() {
|
|
432
|
-
clearTimeout(timer);
|
|
402
|
+
customTimers.clearTimeout.call(undefined, timer);
|
|
433
403
|
timer = undefined;
|
|
434
404
|
};
|
|
435
405
|
return cancelablePromise;
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@common.js/p-timeout",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "Timeout a promise after a specified amount of time",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "etienne-martin/common.js",
|
|
7
7
|
"funding": "https://github.com/sponsors/sindresorhus",
|
|
8
8
|
"type": "commonjs",
|
|
9
|
+
"types": "./index.d.ts",
|
|
9
10
|
"engines": {
|
|
10
|
-
"node": ">=
|
|
11
|
+
"node": ">=14.16"
|
|
11
12
|
},
|
|
12
13
|
"scripts": {
|
|
13
14
|
"test": "xo && ava && tsd"
|
|
@@ -17,13 +18,13 @@
|
|
|
17
18
|
"index.d.ts"
|
|
18
19
|
],
|
|
19
20
|
"devDependencies": {
|
|
20
|
-
"ava": "^3.
|
|
21
|
+
"ava": "^4.3.1",
|
|
21
22
|
"delay": "^5.0.0",
|
|
22
23
|
"in-range": "^3.0.0",
|
|
23
|
-
"p-cancelable": "^
|
|
24
|
-
"time-span": "^
|
|
25
|
-
"tsd": "^0.
|
|
26
|
-
"xo": "^0.
|
|
24
|
+
"p-cancelable": "^4.0.1",
|
|
25
|
+
"time-span": "^5.1.0",
|
|
26
|
+
"tsd": "^0.22.0",
|
|
27
|
+
"xo": "^0.51.0"
|
|
27
28
|
},
|
|
28
29
|
"homepage": "https://github.com/etienne-martin/common.js#readme",
|
|
29
30
|
"dependencies": {},
|
package/readme.md
CHANGED
|
@@ -4,26 +4,27 @@
|
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
7
|
-
```
|
|
8
|
-
|
|
7
|
+
```sh
|
|
8
|
+
npm install p-timeout
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
13
|
```js
|
|
14
|
-
import {setTimeout} from 'timers/promises';
|
|
14
|
+
import {setTimeout} from 'node:timers/promises';
|
|
15
15
|
import pTimeout from 'p-timeout';
|
|
16
16
|
|
|
17
17
|
const delayedPromise = setTimeout(200);
|
|
18
18
|
|
|
19
|
-
await pTimeout(delayedPromise,
|
|
19
|
+
await pTimeout(delayedPromise, {
|
|
20
|
+
milliseconds: 50,
|
|
21
|
+
});
|
|
20
22
|
//=> [TimeoutError: Promise timed out after 50 milliseconds]
|
|
21
23
|
```
|
|
22
24
|
|
|
23
25
|
## API
|
|
24
26
|
|
|
25
|
-
### pTimeout(input,
|
|
26
|
-
### pTimeout(input, milliseconds, fallback?, options?)
|
|
27
|
+
### pTimeout(input, options)
|
|
27
28
|
|
|
28
29
|
Returns a decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout.
|
|
29
30
|
|
|
@@ -35,7 +36,11 @@ Type: `Promise`
|
|
|
35
36
|
|
|
36
37
|
Promise to decorate.
|
|
37
38
|
|
|
38
|
-
####
|
|
39
|
+
#### options
|
|
40
|
+
|
|
41
|
+
Type: `object`
|
|
42
|
+
|
|
43
|
+
##### milliseconds
|
|
39
44
|
|
|
40
45
|
Type: `number`
|
|
41
46
|
|
|
@@ -43,7 +48,7 @@ Milliseconds before timing out.
|
|
|
43
48
|
|
|
44
49
|
Passing `Infinity` will cause it to never time out.
|
|
45
50
|
|
|
46
|
-
|
|
51
|
+
##### message
|
|
47
52
|
|
|
48
53
|
Type: `string | Error`\
|
|
49
54
|
Default: `'Promise timed out after 50 milliseconds'`
|
|
@@ -52,7 +57,7 @@ Specify a custom error message or error.
|
|
|
52
57
|
|
|
53
58
|
If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`.
|
|
54
59
|
|
|
55
|
-
|
|
60
|
+
##### fallback
|
|
56
61
|
|
|
57
62
|
Type: `Function`
|
|
58
63
|
|
|
@@ -61,20 +66,19 @@ Do something other than rejecting with an error on timeout.
|
|
|
61
66
|
You could for example retry:
|
|
62
67
|
|
|
63
68
|
```js
|
|
64
|
-
import {setTimeout} from 'timers/promises';
|
|
69
|
+
import {setTimeout} from 'node:timers/promises';
|
|
65
70
|
import pTimeout from 'p-timeout';
|
|
66
71
|
|
|
67
72
|
const delayedPromise = () => setTimeout(200);
|
|
68
73
|
|
|
69
|
-
await pTimeout(delayedPromise(),
|
|
70
|
-
|
|
74
|
+
await pTimeout(delayedPromise(), {
|
|
75
|
+
milliseconds: 50,
|
|
76
|
+
fallback: () => {
|
|
77
|
+
return pTimeout(delayedPromise(), 300);
|
|
78
|
+
},
|
|
71
79
|
});
|
|
72
80
|
```
|
|
73
81
|
|
|
74
|
-
#### options
|
|
75
|
-
|
|
76
|
-
Type: `object`
|
|
77
|
-
|
|
78
82
|
##### customTimers
|
|
79
83
|
|
|
80
84
|
Type: `object` with function properties `setTimeout` and `clearTimeout`
|
|
@@ -86,7 +90,7 @@ Useful for testing purposes, in particular to work around [`sinon.useFakeTimers(
|
|
|
86
90
|
Example:
|
|
87
91
|
|
|
88
92
|
```js
|
|
89
|
-
import {setTimeout} from 'timers/promises';
|
|
93
|
+
import {setTimeout} from 'node:timers/promises';
|
|
90
94
|
import pTimeout from 'p-timeout';
|
|
91
95
|
|
|
92
96
|
const originalSetTimeout = setTimeout;
|
|
@@ -95,7 +99,8 @@ const originalClearTimeout = clearTimeout;
|
|
|
95
99
|
sinon.useFakeTimers();
|
|
96
100
|
|
|
97
101
|
// Use `pTimeout` without being affected by `sinon.useFakeTimers()`:
|
|
98
|
-
await pTimeout(doSomething(),
|
|
102
|
+
await pTimeout(doSomething(), {
|
|
103
|
+
milliseconds: 2000,
|
|
99
104
|
customTimers: {
|
|
100
105
|
setTimeout: originalSetTimeout,
|
|
101
106
|
clearTimeout: originalClearTimeout
|
|
@@ -123,7 +128,8 @@ setTimeout(() => {
|
|
|
123
128
|
abortController.abort();
|
|
124
129
|
}, 100);
|
|
125
130
|
|
|
126
|
-
await pTimeout(delayedPromise,
|
|
131
|
+
await pTimeout(delayedPromise, {
|
|
132
|
+
milliseconds: 2000,
|
|
127
133
|
signal: abortController.signal
|
|
128
134
|
});
|
|
129
135
|
```
|