You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 '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(), 2000, undefined, {
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
readonlycustomTimers?: {
41
-
setTimeout: typeofglobal.setTimeout;
42
-
clearTimeout: typeofglobal.clearTimeout;
78
+
setTimeout: typeofglobalThis.setTimeout;
79
+
clearTimeout: typeofglobalThis.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, 2000, undefined, {
100
+
await pTimeout(delayedPromise, {
101
+
milliseconds: 2000,
64
102
signal: abortController.signal
65
103
});
66
104
```
@@ -74,36 +112,6 @@ 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
-
exportdefaultfunctionpTimeout<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
@@ -113,14 +121,15 @@ import pTimeout from 'p-timeout';
0 commit comments