0.4.3 • Published 2 years ago
extra-retry v0.4.3
extra-retry
Yet another retry library, but functional style.
Install
npm install --save extra-retry
# or
yarn add extra-retryUsage
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): IPredicateexponentialBackoff
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:
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.
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
2 years ago
0.3.6
3 years ago
0.4.1
3 years ago
0.4.0
3 years ago
0.4.2
3 years ago
0.3.5
3 years ago
0.3.4
3 years ago
0.3.3
3 years ago
0.3.2
4 years ago
0.2.10
4 years ago
0.3.0
4 years ago
0.3.1
4 years ago
0.2.9
4 years ago
0.2.8
4 years ago
0.2.7
4 years ago
0.2.6
4 years ago
0.2.3
5 years ago
0.2.5
4 years ago
0.2.4
4 years ago
0.2.2
5 years ago
0.2.1
5 years ago
0.2.0
5 years ago
0.1.0
5 years ago