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
2 changes: 1 addition & 1 deletion async/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@std/async",
"version": "1.0.16",
"version": "1.1.0",
"exports": {
".": "./mod.ts",
"./abortable": "./abortable.ts",
Expand Down
39 changes: 39 additions & 0 deletions async/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ export interface RetryOptions {
* @default {1}
*/
jitter?: number;
/**
* Callback to determine if an error or other thrown value is retriable.
*
* @default {() => true}
*
* @param err The thrown error or other value.
* @returns `true` if the error is retriable, `false` otherwise.
*/
isRetriable?: (err: unknown) => boolean;
}

/**
Expand Down Expand Up @@ -112,10 +121,35 @@ export interface RetryOptions {
* });
* ```
*
* @example Only retry on specific error types
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice example

* ```ts no-assert
* import { retry } from "@std/async/retry";
*
* class HttpError extends Error {
* status: number;
* constructor(status: number) {
* super(`HTTP ${status}`);
* this.status = status;
* }
* }
*
* const req = async () => {
* // some function that throws HttpError
* };
*
* // Only retry on 429 (rate limit) or 5xx (server) errors
* const retryPromise = await retry(req, {
* isRetriable: (err) =>
* err instanceof HttpError && (err.status === 429 || err.status >= 500),
* });
* ```
*
* @typeParam T The return type of the function to retry and returned promise.
* @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 `isRetriable` returns `false` for an error, throws that error immediately.
*/
export async function retry<T>(
fn: (() => Promise<T>) | (() => T),
Expand All @@ -127,6 +161,7 @@ export async function retry<T>(
maxAttempts = 5,
minTimeout = 1000,
jitter = 1,
isRetriable = () => true,
} = options ?? {};

if (!Number.isInteger(maxAttempts) || maxAttempts < 1) {
Expand Down Expand Up @@ -165,6 +200,10 @@ export async function retry<T>(
try {
return await fn();
} catch (error) {
if (!isRetriable(error)) {
throw error;
}

if (attempt + 1 >= maxAttempts) {
throw new RetryError(error, maxAttempts);
}
Expand Down
43 changes: 43 additions & 0 deletions async/retry_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,46 @@ Deno.test("retry() caps backoff at maxTimeout", async () => {

await assertRejects(() => promise, RetryError);
});

Deno.test("retry() only retries errors that are retriable with `isRetriable` option", async () => {
class HttpError extends Error {
status: number;
constructor(status: number) {
super();
this.status = status;
}
}

const isRetriable = (err: unknown) =>
err instanceof HttpError && (err.status === 429 || err.status >= 500);

const options = {
minTimeout: 1,
isRetriable,
};

let numCalls: number;

numCalls = 0;
await assertRejects(() =>
retry(() => {
numCalls++;
throw new HttpError(400);
}, options), HttpError);
assertEquals(numCalls, 1);

numCalls = 0;
await assertRejects(() =>
retry(() => {
numCalls++;
throw new HttpError(500);
}, options), RetryError);
assertEquals(numCalls, 5);

numCalls = 0;
await assertRejects(() =>
retry(() => {
throw new HttpError(++numCalls === 3 ? 400 : 500);
}, options), HttpError);
assertEquals(numCalls, 3);
});
3 changes: 1 addition & 2 deletions import_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
"npm:/typescript": "npm:typescript@5.8.2",
"automation/": "https://raw.githubusercontent.com/denoland/automation/0.10.0/",
"graphviz": "npm:node-graphviz@^0.1.1",

"@std/assert": "jsr:@std/assert@^1.0.16",
"@std/async": "jsr:@std/async@^1.0.16",
"@std/async": "jsr:@std/async@^1.1.0",
"@std/bytes": "jsr:@std/bytes@^1.0.6",
"@std/cache": "jsr:@std/cache@^0.2.1",
"@std/cbor": "jsr:@std/cbor@^0.1.9",
Expand Down
Loading