Skip to content

Commit e5e9571

Browse files
authored
EB-3 Making first argument just the function to backoff (#6)
1 parent 322791d commit e5e9571

8 files changed

+2398
-3228
lines changed

README.md

+31-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# exponential-backoff
2+
23
A utility that allows retrying a function with an exponential delay between attempts.
34

45
## Installation
@@ -8,52 +9,51 @@ npm i exponential-backoff
89
```
910

1011
## 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>`.
1214

1315
```
14-
function backOff<T>(request: IBackOffRequest<T>, options: Partial<IBackOffOptions> = {}): Promise<T>
16+
function backOff<T>(request: () => Promise<T>, options: IBackOffOptions = {}): Promise<T>
1517
```
1618

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).
1920

20-
The function to be attempted.
21+
### `IBackOffOptions`
2122

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`
2724

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.
3038

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.
39+
Default value is `10`.
3240

33-
Default value is `true`.
41+
Minimum value is `1`.
3442

35-
* `jitter?: JitterType | string`
43+
- `retry?: (e: any, attemptNumber: number) => boolean`
3644

37-
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.
3846

39-
Default value is `none`.
47+
Default value is a function that always returns `true`.
4048

41-
* `numOfAttempts?: number`
49+
- `startingDelay?: number`
4250

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.
4852

49-
* `startingDelay?: number`
53+
Default value is `100` ms.
5054

51-
The delay before executing the function for the first time.
52-
53-
Default value is `100` ms.
55+
- `timeMultiple?: number`
5456

55-
* `timeMultiple?: number`
57+
The `startingDelay` is multiplied by the `timeMultiple` to increase the delay between reattempts.
5658

57-
The `startingDelay` is multiplied by the `timeMultiple` value to increase the delay between reattempts.
58-
59-
Default value is `2`.
59+
Default value is `2`.

doc/v1-to-v2-migration.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Migrating from v1 to v2
2+
3+
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`.
8+
9+
That's all folks!

0 commit comments

Comments
 (0)