0.4.3 • Published 10 months ago

extra-retry v0.4.3

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

extra-retry

Yet another retry library, but functional style.

Install

npm install --save extra-retry
# or
yarn add extra-retry

Usage

import { retryUntil, anyOf, maxRetries, delay } from 'extra-retry'
import ms from 'ms'

await retryUntil(
  anyOf(
    maxRetries(3)
  , delay(ms('5s'))
  )
, fn
)

API

interface IContext {
  error: unknown
  retries: number // the number of retries, starting from 0.
}

type IPredicate<T = unknown> = (context: IContext) => T | PromiseLike<T>

retryUntil

function retryUntil(predicate: IPredicate): <T>(fn: () => Awaitable<T>) => Promise<T>
function retryUntil<T>(predicate: IPredicate, fn: () => Awaitable<T>): Promise<T>

If fn throws an error, retry until the return value of the predicate is Truthy.

Helpers

anyOf

function anyOf(...predicates: Array<IPredicate | Falsy>): IPredicate<boolean>

Equivalent to

context => (predicate1 && await predicate1(context))
        || (predicate2 && await predicate2(context))
        || ...
        || (predicateN && await predicateN(context))

delay

function delay(ms: number): IPredicate

exponentialBackoff

function exponentialBackoff({
  baseTimeout
, maxTimeout = Infinity
, factor = 2
, jitter = true
}: {
  baseTimeout: number
  maxTimeout?: number
  factor?: number
  jitter?: boolean
}): IPredicate<boolean>

Equivalent to

const timeout = Math.min(factor ** retries * baseTimeout, maxTimeout)
delay(jitter ? randomIntInclusive(0, timeout) : timeout)

maxRetries

function maxRetries(times: number): IPredicate<boolean>

notRetryOn

function notRetryOn(errors: Array<Constructor<Error>>): IPredicate<boolean>

Blacklist.

notRetryOnCommonFatalErrors

const notRetryOnCommonFatalErrors: IPredicate<boolean>

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

function retryOn(errors: Array<Constructor<Error>>): IPredicate<boolean>

Whitelist.

signal

function signal(abortSignal: AbortSignal): IPredicate<boolean>

tap

function tap(fn: (context: IContext) => void): IPredicate<false>
0.4.3

10 months ago

0.3.6

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.4.2

1 year ago

0.3.5

1 year ago

0.3.4

1 year ago

0.3.3

2 years ago

0.3.2

2 years ago

0.2.10

2 years ago

0.3.0

2 years ago

0.3.1

2 years ago

0.2.9

2 years ago

0.2.8

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.3

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago