Skip to main content

@mr/retry@0.0.3
Built and signed on GitHub Actions

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 weeks ago (0.0.3)
function retry
retry<Fn extends (...args: any[]) => any>(
f: Fn,
opts?: Config,
): (...args: Parameters<Fn>) => Promise<ReturnType<Fn>>

Exponentially retry a function until it succeeds or the maximum number of attempts is reached.

Each delay will be calculated as delay * 2 ** c, where c is the current attempt number, and an applied jitter.

Examples

Example 1

let fn = retry(
	async (url: string) => {
   let res = await fetch(url);
   if (!res.ok) throw new Error(await res.text());
   return await res.json();
});

let data = await fn('https://api.example.com/data');

Type Parameters

Fn extends (...args: any[]) => any

Parameters

f: Fn
optional
opts: Config

Return Type

(...args: Parameters<Fn>) => Promise<ReturnType<Fn>>

Add Package

deno add @mr/retry

Import symbol

import { retry } from "@mr/retry";

---- OR ----

Import directly with a jsr specifier

import { retry } from "jsr:@mr/retry";

Add Package

npx jsr add @mr/retry

Import symbol

import { retry } from "@mr/retry";

Add Package

yarn dlx jsr add @mr/retry

Import symbol

import { retry } from "@mr/retry";

Add Package

pnpm dlx jsr add @mr/retry

Import symbol

import { retry } from "@mr/retry";

Add Package

bunx jsr add @mr/retry

Import symbol

import { retry } from "@mr/retry";