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
importgotfrom'got'importwithRetryfrom'@teneff/with-retry'
@withRetry({maxCalls: 5,errors: [got.TimeoutError],})exportdefaultfunctiongetFlakyServiceData(){returnawaitgot("https://example.com");}using got
importgotfrom"got";importwithRetryfrom"@teneff/with-retry";functiongetFlakyServiceData(){returnawaitgot("https://example.com");}exportdefaultwithRetry({maxCalls: 5,errors: [got.TimeoutError],})(getFlakyServiceData);importgotfrom"got";importwithRetryfrom'@teneff/with-retry/decorator'classExample
@withRetry({maxCalls: 5,errors: [got.TimeoutError],})getFlakyServiceData(){returnawaitgot("https://example.com");}}Adds support for unknown errors
importwithRetry,{UnknownError}from"@teneff/with-retry";functionresolvesPromiseWithNonError(){returnPromise.reject("a string");}awaitwithRetry({errors: UnknownError,})(resolvesPromiseWithNonError)();Fixes issue #6 preserving class context
classExample{privatemockCallback=jest.fn().mockRejectedValueOnce(newError(`[${num}] mock error`)).mockResolvedValue(`[${num}] success`);
@withRetry({maxCalls: 4,})getData2(): Promise<string>{returnthis.mockCallback("arg1","arg2");}}