Retry async functions when error happens
Type: object
Type: number (default: 2)
Specifies the maximum amount of calls to the decorated function.
Type: Error[] (default: [Error])
Specifies an array of errors for which the function should be retried. If the default option is used it will be retried for every error.
Type: number | ({ call: number; errors: Error[] }) => number (default: 0)
Specifies amount of delay before each retry.
- If a
numberis given after each Error the subsequent invocation will be delayed with a fixed amount. - If a
functionreturningnumberis given it will invoke the function and delay the invocations by the result
as Babel legacy decorator
on request timeout using got
import got from 'got'
import withRetry from '@teneff/with-retry'
@withRetry({
maxCalls: 5,
errors: [got.TimeoutError],
})
export default function getFlakyServiceData() {
return await got("https://example.com");
}using got
import got from "got";
import withRetry from "@teneff/with-retry";
function getFlakyServiceData() {
return await got("https://example.com");
}
export default withRetry({
maxCalls: 5,
errors: [got.TimeoutError],
})(getFlakyServiceData);import got from "got";
import withRetry from '@teneff/with-retry/decorator'
class Example
@withRetry({
maxCalls: 5,
errors: [got.TimeoutError],
})
getFlakyServiceData() {
return await got("https://example.com");
}
}Adds support for unknown errors
import withRetry, { UnknownError } from "@teneff/with-retry";
function resolvesPromiseWithNonError() {
return Promise.reject("a string");
}
await withRetry({
errors: UnknownError,
})(resolvesPromiseWithNonError)();Fixes issue #6 preserving class context
class Example {
private mockCallback = jest
.fn()
.mockRejectedValueOnce(new Error(`[${num}] mock error`))
.mockResolvedValue(`[${num}] success`);
@withRetry({
maxCalls: 4,
})
getData2(): Promise<string> {
return this.mockCallback("arg1", "arg2");
}
}