New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@teneff/with-retry

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@teneff/with-retry

Decorator for retrying async operations

  • 1.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@teneff/with-retry

Retry async functions when error happens

NPM version Build Status Coverage Status GitHub issues GitHub 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:

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

as a function

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

as an experimental decorator

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

v1.1.0

Adds support for unknown errors

import withRetry, { UnknownError } from "@teneff/with-retry";

function resolvesPromiseWithNonError() {
  return Promise.reject("a string");
}

await withRetry({
  errors: UnknownError,
})(resolvesPromiseWithNonError)();

v1.1.2

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

Keywords

FAQs

Package last updated on 23 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc