Skip to main content
Home

Built and signed on GitHub Actions

Works with
It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score88%
Downloads48/wk
Published2 years ago (2.0.0)

safe-retry

Type-safe retry utility with exponential backoff support. For a more in-depth overview on how this can be helpful, read the following blogpost: Retrying API Calls with Exponential Backoff in JavaScript.

Install safe-retry from npm

npm install safe-retry --save

How to use it

Have a look at the sample project for a demo, but here is the minimal usage of the retry function:

import { retry } from "safe-retry";

const result = await retry(
  () => {
    console.log("Something that returns a promise");
    return Promise.reject();
  },
  {
    maxRetries: 4,
    onRetry: (retryAttempt, timeToWait) => {
      console.log(
        `Waiting for ${timeToWait}ms before next attempt. Attempt: ${retryAttempt}`
      );
    },
  }
);

In the example above, if the promise given as the first argument fails, it will be retries at least 4 times given the maxRetries option, and it will exponentially wait on every failed attempt, until it either reaches its maximum retry limit or resolves.

Built and signed on
GitHub Actions

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@bpaulino/safe-retry

Import symbol

import * as safe_retry from "@bpaulino/safe-retry";
or

Import directly with a jsr specifier

import * as safe_retry from "jsr:@bpaulino/safe-retry";

Add Package

pnpm i jsr:@bpaulino/safe-retry
or (using pnpm 10.8 or older)
pnpm dlx jsr add @bpaulino/safe-retry

Import symbol

import * as safe_retry from "@bpaulino/safe-retry";

Add Package

yarn add jsr:@bpaulino/safe-retry
or (using Yarn 4.8 or older)
yarn dlx jsr add @bpaulino/safe-retry

Import symbol

import * as safe_retry from "@bpaulino/safe-retry";

Add Package

vlt install jsr:@bpaulino/safe-retry

Import symbol

import * as safe_retry from "@bpaulino/safe-retry";

Add Package

npx jsr add @bpaulino/safe-retry

Import symbol

import * as safe_retry from "@bpaulino/safe-retry";

Add Package

bunx jsr add @bpaulino/safe-retry

Import symbol

import * as safe_retry from "@bpaulino/safe-retry";