Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions async/unstable_retry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2026 the Deno authors. MIT license.
// This module is browser compatible.

import { delay } from "./delay.ts";
import { exponentialBackoffWithJitter } from "./_util.ts";
import { RetryError } from "./retry.ts";

Expand Down Expand Up @@ -51,6 +52,17 @@ export interface RetryOptions {
* @returns `true` if the error is retriable, `false` otherwise.
*/
isRetriable?: (err: unknown) => boolean;

/**
* An AbortSignal to cancel the retry operation.
*
* If the signal is aborted, the retry will stop and reject with the signal's
* reason. The signal is checked before each attempt and during the delay
* between attempts.
*
* @default {undefined}
*/
signal?: AbortSignal;
}

/**
Expand Down Expand Up @@ -108,6 +120,9 @@ export interface RetryOptions {
* @param fn The function to retry.
* @param options Additional options.
* @returns The promise that resolves with the value returned by the function to retry.
* @throws {RetryError} If the function fails after `maxAttempts` attempts.
* @throws If the `signal` is aborted, throws the signal's reason.
* @throws If `isRetriable` returns `false` for an error, throws that error immediately.
*/
export async function retry<T>(
fn: (() => Promise<T>) | (() => T),
Expand All @@ -120,26 +135,46 @@ export async function retry<T>(
minTimeout = 1000,
jitter = 1,
isRetriable = () => true,
signal,
} = options ?? {};

if (maxTimeout <= 0) {
throw new TypeError(
`Cannot retry as 'maxTimeout' must be positive: current value is ${maxTimeout}`,
if (!Number.isInteger(maxAttempts) || maxAttempts < 1) {
throw new RangeError(
`Cannot retry as 'maxAttempts' must be a positive integer: current value is ${maxAttempts}`,
);
}
if (!Number.isFinite(multiplier) || multiplier < 1) {
throw new RangeError(
`Cannot retry as 'multiplier' must be a finite number >= 1: current value is ${multiplier}`,
);
}
if (Number.isNaN(maxTimeout) || maxTimeout <= 0) {
throw new RangeError(
`Cannot retry as 'maxTimeout' must be a positive number: current value is ${maxTimeout}`,
);
}
if (Number.isNaN(minTimeout) || minTimeout < 0) {
throw new RangeError(
`Cannot retry as 'minTimeout' must be >= 0: current value is ${minTimeout}`,
);
}
if (minTimeout > maxTimeout) {
throw new TypeError(
throw new RangeError(
`Cannot retry as 'minTimeout' must be <= 'maxTimeout': current values 'minTimeout=${minTimeout}', 'maxTimeout=${maxTimeout}'`,
);
}
if (jitter > 1) {
throw new TypeError(
`Cannot retry as 'jitter' must be <= 1: current value is ${jitter}`,
if (Number.isNaN(jitter) || jitter < 0 || jitter > 1) {
throw new RangeError(
`Cannot retry as 'jitter' must be between 0 and 1: current value is ${jitter}`,
);
}

let attempt = 0;
while (true) {
if (signal?.aborted) {
throw signal.reason;
}

try {
return await fn();
} catch (error) {
Expand All @@ -158,7 +193,7 @@ export async function retry<T>(
multiplier,
jitter,
);
await new Promise((r) => setTimeout(r, timeout));
await delay(timeout, signal ? { signal } : undefined);
}
attempt++;
}
Expand Down
Loading
Loading