Yet another retry library, but functional style.
npm install --save extra-retry
# or
yarn add extra-retryimport { retryUntil, anyOf, maxRetries, delay } from 'extra-retry'
import ms from 'ms'
await retryUntil(
anyOf(
maxRetries(3)
, delay(ms('5s'))
)
, fn
)interface IContext {
error: unknown
retries: number // the number of retries, starting from 0.
}
type IPredicate<T = unknown> = (context: IContext) => Awaitable<T>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 fn until the return value of the predicate is Truthy.
function retryIterableUntil(
predicate: IPredicate
): <T>(
fn: () => Awaitable<Iterable<T> | AsyncIterable<T>>
) => AsyncIterableIterator<Awaited<T>>
function retryIterableUntil<T>(
predicate: IPredicate
, fn: () => Awaitable<Iterable<T> | AsyncIterable<T>>
): AsyncIterableIterator<Awaited<T>>If fn or the Iterable/AsyncIterable returned by it throws an error,
retry fn until the return value of the predicate is Truthy.
function withRetryUntil(
predicate: IPredicate
): <Args extends unknown[], Result>(
fn: (...args: Args) => Awaitable<Result>
) => (...args: Args) => Promise<Result>
function withRetryUntil<Args extends unknown[], Result>(
predicate: IPredicate
, fn: (...args: Args) => Awaitable<Result>
): (...args: Args) => Promise<Result>A simple wrapper for retryUntil.
function withRetryIterableUntil(
predicate: IPredicate
): <Args extends unknown[], Value>(
fn: (...args: Args) => Awaitable<Iterable<Value> | AsyncIterable<Value>>
) => (...args: Args) => AsyncIterableIterator<Awaited<Value>>
function withRetryIterableUntil<Args extends unknown[], Value>(
predicate: IPredicate
, fn: (...args: Args) => Awaitable<Iterable<Value> | AsyncIterable<Value>>
): (...args: Args) => AsyncIterableIterator<Awaited<Value>>A simple wrapper for retryIterableUntil.
function anyOf(...predicates: Array<IPredicate | Falsy>): IPredicate<boolean>Equivalent to
context => (predicate1 && await predicate1(context))
|| (predicate2 && await predicate2(context))
|| ...
|| (predicateN && await predicateN(context))function delay(ms: number): IPredicatefunction 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)function maxRetries(times: number): IPredicate<boolean>function notRetryOn(errors: Array<Constructor<Error>>): IPredicate<boolean>Blacklist.
const notRetryOnCommonFatalErrors: IPredicate<boolean>This predicate blacklists theses errors:
SyntaxErrorReferenceErrorRangeErrorURIError
There is no TypeError because TypeError does not mean
"a value is not of the expected type",
it has been abused for various purposes.
function retryOn(errors: Array<Constructor<Error>>): IPredicate<boolean>Whitelist.
function signal(abortSignal: AbortSignal): IPredicate<boolean>function tap(fn: (context: IContext) => void): IPredicate<false>