basant307/AI_Governance_Project
048
1# exponential-backoff2 3A utility that allows retrying a function with an exponential delay between attempts.4 5## Installation6 7```8npm i exponential-backoff9```10 11## Usage12 13The `backOff<T>` function takes a promise-returning function to retry, and an optional `BackOffOptions` object. It returns a `Promise<T>`.14 15```ts16function backOff<T>(17 request: () => Promise<T>,18 options?: BackOffOptions19): Promise<T>;20```21 22Here is an example retrying a function that calls a hypothetical weather endpoint:23 24```js25import { backOff } from "exponential-backoff";26 27function getWeather() {28 return fetch("weather-endpoint");29}30 31async function main() {32 try {33 const response = await backOff(() => getWeather());34 // process response35 } catch (e) {36 // handle error37 }38}39 40main();41```42 43Migrating across major versions? Here are our [breaking changes](https://github.com/coveo/exponential-backoff/tree/master/doc/migration-guide.md).44 45### `BackOffOptions`46 47- `delayFirstAttempt?: boolean`48 49 Decides whether the `startingDelay` should be applied before the first call. If `false`, the first call will occur without a delay.50 51 Default value is `false`.52 53- `jitter?: JitterType | string`54 55 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`.56 57 Default value is `none`.58 59- `maxDelay?: number`60 61 The maximum delay, in milliseconds, between two consecutive attempts.62 63 Default value is `Infinity`.64 65- `numOfAttempts?: number`66 67 The maximum number of times to attempt the function.68 69 Default value is `10`.70 71 Minimum value is `1`.72 73- `retry?: (e: any, attemptNumber: number) => boolean | Promise<boolean>`74 75 The `retry` function can be used to run logic after every failed attempt (e.g. logging a message, assessing the last error, etc.). It is called with the last error and the upcoming attempt number. Returning `true` will retry the function as long as the `numOfAttempts` has not been exceeded. Returning `false` will end the execution.76 77 Default value is a function that always returns `true`.78 79- `startingDelay?: number`80 81 The delay, in milliseconds, before executing the function for the first time.82 83 Default value is `100` ms.84 85- `timeMultiple?: number`86 87 The `startingDelay` is multiplied by the `timeMultiple` to increase the delay between reattempts.88 89 Default value is `2`.90 