Skip to content

Repository files navigation

@teneff/with-retry

Retry async functions when error happens

NPM versionBuild StatusCoverage StatusGitHub issuesGitHub stars

Options

Type: object

options.maxCalls

Type: number(default: 2)

Specifies the maximum amount of calls to the decorated function.

options.errors

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.

options.delay

Type: number | ({ call: number; errors: Error[] }) => number(default: 0)

Specifies amount of delay before each retry.

  • If a number is given after each Error the subsequent invocation will be delayed with a fixed amount.
  • If a function returning number is given it will invoke the function and delay the invocations by the result

Examples:

on request timeout using got

importgotfrom'got'importwithRetryfrom'@teneff/with-retry'
@withRetry({maxCalls: 5,errors: [got.TimeoutError],})exportdefaultfunctiongetFlakyServiceData(){returnawaitgot("https://example.com");}

as a function

using got

importgotfrom"got";importwithRetryfrom"@teneff/with-retry";functiongetFlakyServiceData(){returnawaitgot("https://example.com");}exportdefaultwithRetry({maxCalls: 5,errors: [got.TimeoutError],})(getFlakyServiceData);

as an experimental decorator

importgotfrom"got";importwithRetryfrom'@teneff/with-retry/decorator'classExample
@withRetry({maxCalls: 5,errors: [got.TimeoutError],})getFlakyServiceData(){returnawaitgot("https://example.com");}}

v1.1.0

Adds support for unknown errors

importwithRetry,{UnknownError}from"@teneff/with-retry";functionresolvesPromiseWithNonError(){returnPromise.reject("a string");}awaitwithRetry({errors: UnknownError,})(resolvesPromiseWithNonError)();

v1.1.2

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");}}

About

Decorator for retrying async operations

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages