https://github.com/blackglory/extra-retry
🌲 Yet another retry library, but functional style
https://github.com/blackglory/extra-retry
browser esm library nodejs npm-package typescript
Last synced: 2 months ago
JSON representation
🌲 Yet another retry library, but functional style
- Host: GitHub
- URL: https://github.com/blackglory/extra-retry
- Owner: BlackGlory
- License: mit
- Created: 2021-03-22T08:22:24.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-06-11T02:42:42.000Z (about 3 years ago)
- Last Synced: 2025-02-03T20:07:16.255Z (over 1 year ago)
- Topics: browser, esm, library, nodejs, npm-package, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/extra-retry
- Size: 893 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# extra-retry
Yet another retry library, but functional style.
## Install
```sh
npm install --save extra-retry
# or
yarn add extra-retry
```
## Usage
```ts
import { retryUntil, anyOf, maxRetries, delay } from 'extra-retry'
import ms from 'ms'
await retryUntil(
anyOf(
maxRetries(3)
, delay(ms('5s'))
)
, fn
)
```
## API
```ts
interface IContext {
error: unknown
retries: number // the number of retries, starting from 0.
}
type IPredicate = (context: IContext) => Awaitable
```
### retryUntil
```ts
function retryUntil(predicate: IPredicate): (fn: () => Awaitable) => Promise
function retryUntil(predicate: IPredicate, fn: () => Awaitable): Promise
```
If `fn` throws an error,
retry until the return value of the `predicate` is [Truthy].
[Truthy]: https://developer.mozilla.org/en-US/docs/Glossary/Truthy
### withRetryUntil
```ts
function withRetryUntil(
predicate: IPredicate
): (
fn: (...args: Args) => Awaitable
) => (...args: Args) => Promise
function withRetryUntil(
predicate: IPredicate
, fn: (...args: Args) => Awaitable
): (...args: Args) => Promise
```
A simple wrapper around `retryUntil`.
### Helpers
#### anyOf
```ts
function anyOf(...predicates: Array): IPredicate
```
Equivalent to
```ts
context => (predicate1 && await predicate1(context))
|| (predicate2 && await predicate2(context))
|| ...
|| (predicateN && await predicateN(context))
```
#### delay
```ts
function delay(ms: number): IPredicate
```
#### exponentialBackoff
```ts
function exponentialBackoff({
baseTimeout
, maxTimeout = Infinity
, factor = 2
, jitter = true
}: {
baseTimeout: number
maxTimeout?: number
factor?: number
jitter?: boolean
}): IPredicate
```
Equivalent to
```ts
const timeout = Math.min(factor ** retries * baseTimeout, maxTimeout)
delay(jitter ? randomIntInclusive(0, timeout) : timeout)
```
#### maxRetries
```ts
function maxRetries(times: number): IPredicate
```
#### notRetryOn
```ts
function notRetryOn(errors: Array>): IPredicate
```
Blacklist.
#### notRetryOnCommonFatalErrors
```ts
const notRetryOnCommonFatalErrors: IPredicate
```
This predicate blacklists theses errors:
- `SyntaxError`
- `ReferenceError`
- `RangeError`
- `URIError`
There is no `TypeError` because `TypeError` does not mean
"a value is not of the expected type",
it has been abused for various purposes.
#### retryOn
```ts
function retryOn(errors: Array>): IPredicate
```
Whitelist.
#### signal
```ts
function signal(abortSignal: AbortSignal): IPredicate
```
#### tap
```ts
function tap(fn: (context: IContext) => void): IPredicate
```