Skip to content

Commit 099a178

Browse files
authored
docs: add TSDoc comments (#68)
This makes the developer experience a bit nicer: the IDE will display the documentation inline for each of the options and the backOff function itself. The values in the comments were sourced from the README and only slightly reformatted.
1 parent 01db04d commit 099a178

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/backoff.ts

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import { DelayFactory } from "./delay/delay.factory";
77

88
export { BackoffOptions, IBackOffOptions };
99

10+
/**
11+
* Executes a function with exponential backoff.
12+
* @param request the function to be executed
13+
* @param options options to customize the backoff behavior
14+
* @returns Promise that resolves to the result of the `request` function
15+
*/
1016
export async function backOff<T>(
1117
request: () => Promise<T>,
1218
options: BackoffOptions = {}

src/options.ts

+43
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,57 @@
1+
/**
2+
* Type of jitter to apply to the delay.
3+
* - `"none"`: no jitter is applied
4+
* - `"full"`: full jitter is applied (random value between `0` and `delay`)
5+
*/
16
export type JitterType = "none" | "full";
27

38
export type BackoffOptions = Partial<IBackOffOptions>;
49

510
export interface IBackOffOptions {
11+
/**
12+
* Decides whether the `startingDelay` should be applied before the first call.
13+
* If `false`, the first call will occur without a delay.
14+
* @defaultValue `false`
15+
*/
616
delayFirstAttempt: boolean;
17+
/**
18+
* Decides whether a [jitter](https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/)
19+
* should be applied to the delay. Possible values are `"full"` and `"none"`.
20+
* @defaultValue `"none"`
21+
*/
722
jitter: JitterType;
23+
/**
24+
* The maximum delay, in milliseconds, between two consecutive attempts.
25+
* @defaultValue `Infinity`
26+
*/
827
maxDelay: number;
28+
/**
29+
* The maximum number of times to attempt the function.
30+
* Must be at least `1`.
31+
* @defaultValue `10`
32+
*/
933
numOfAttempts: number;
34+
/**
35+
* The `retry` function can be used to run logic after every failed attempt (e.g. logging a message,
36+
* assessing the last error, etc.).
37+
* It is called with the last error and the upcoming attempt number.
38+
* Returning `true` will retry the function as long as the `numOfAttempts` has not been exceeded.
39+
* Returning `false` will end the execution.
40+
* @defaultValue a function that always returns `true`.
41+
* @param e The last error thrown by the function.
42+
* @param attemptNumber The upcoming attempt number.
43+
* @returns `true` to retry the function, `false` to end the execution
44+
*/
1045
retry: (e: any, attemptNumber: number) => boolean | Promise<boolean>;
46+
/**
47+
* The delay, in milliseconds, before executing the function for the first time.
48+
* @defaultValue `100`
49+
*/
1150
startingDelay: number;
51+
/**
52+
* The `startingDelay` is multiplied by the `timeMultiple` to increase the delay between reattempts.
53+
* @defaultValue `2`
54+
*/
1255
timeMultiple: number;
1356
}
1457

0 commit comments

Comments
 (0)