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
A utility that allows retrying a function with an exponential delay between attempts.
3
4
4
5
## Installation
@@ -8,52 +9,51 @@ npm i exponential-backoff
8
9
```
9
10
10
11
## Usage
11
-
The generic `backOff<T>` function takes an `IBackOffRequest<T>` object, and an optional `IBackOffOptions` object. It returns a generic `Promise<T>`.
12
+
13
+
The generic `backOff<T>` function takes a function `() => Promise<T>` to be retried, and an optional `IBackOffOptions` object. It returns a generic `Promise<T>`.
12
14
13
15
```
14
-
function backOff<T>(request: IBackOffRequest<T>, options: Partial<IBackOffOptions> = {}): Promise<T>
16
+
function backOff<T>(request: () => Promise<T>, options: IBackOffOptions = {}): Promise<T>
15
17
```
16
18
17
-
### `IBackOffRequest<T>`
18
-
*`fn: () => Promise<T>`
19
+
Migrating from v1 to v2? Here are our [breaking changes](https://github.com/coveo/exponential-backoff/tree/master/doc/v1-to-v2-migration.md).
19
20
20
-
The function to be attempted.
21
+
### `IBackOffOptions`
21
22
22
-
*`retry?: (e, attemptNumber: number) => boolean`
23
-
24
-
Everytime `fn` returns a rejected promise, the `retry` function is called with the error and the attempt number. Returning `true` will reattempt the function as long as the `numOfAttempts` has not been exceeded. Returning `false` will end the execution.
25
-
26
-
Default value is a function that always returns `true`.
23
+
-`delayFirstAttempt?: boolean`
27
24
28
-
### `IBackOffOptions`
29
-
*`delayFirstAttempt?: boolean`
25
+
Decides whether the `startingDelay` should be applied before the first call. If `false`, the first call will occur without a delay.
26
+
27
+
Default value is `false`.
28
+
29
+
-`jitter?: JitterType | string`
30
+
31
+
Decides whether a [jitter](https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/) should be applied to the delay. Possible values are `full` and `none`.
32
+
33
+
Default value is `none`.
34
+
35
+
-`numOfAttempts?: number`
36
+
37
+
The maximum number of times to attempt the function.
30
38
31
-
Decides whether the `startingDelay` should be applied before the first call to `fn`. If `false`, the first call to `fn` will occur without a delay.
Decides whether a [jitter](https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/) should be applied to the delay. Possible values are `full` and `none`.
45
+
Everytime the function returns a rejected promise, the `retry` function is called with the error and the attempt number. Returning `true` will reattempt the function as long as the `numOfAttempts` has not been exceeded. Returning `false` will end the execution.
38
46
39
-
Default value is `none`.
47
+
Default value is a function that always returns `true`.
40
48
41
-
*`numOfAttempts?: number`
49
+
-`startingDelay?: number`
42
50
43
-
The maximum number of times to attempt the function.
44
-
45
-
Default value is `10`.
46
-
47
-
Minimum value is `1`.
51
+
The delay before executing the function for the first time.
48
52
49
-
*`startingDelay?: number`
53
+
Default value is `100` ms.
50
54
51
-
The delay before executing the function for the first time.
52
-
53
-
Default value is `100` ms.
55
+
-`timeMultiple?: number`
54
56
55
-
*`timeMultiple?: number`
57
+
The `startingDelay` is multiplied by the `timeMultiple` to increase the delay between reattempts.
56
58
57
-
The `startingDelay` is multiplied by the `timeMultiple` value to increase the delay between reattempts.
If you are migrating from v1 to v2, be aware of the following breaking changes:
4
+
5
+
- The first argument of `backOff<T>` is now just the function you want to backoff. The `retry` function is still available as a property of `IBackOffOptions`.
6
+
7
+
- The default value for the `delayFirstAttempt` option is now `false`.
0 commit comments