Retries the given function the specified number of times.
const result = await retry({ times: 3, delay: 1000 }, async () => { return await fetch("https://example.com");});
npx atmx add helper retry
Copy and paste the following method into @/utils/helpers/undefined.ts:
@/utils/helpers/undefined.ts
import { sleep } from "@/helpers/sleep.ts";import { tryit } from "@/helpers/tryit.ts"; export type RetryOptions = {times?: number;delay?: number | null;backoff?: (count: number) => number;}; /*** Retries the given function the specified number of times.** @example* const result = await retry({ times: 3, delay: 1000 }, async () => {* return await fetch('https://example.com')* })*/export async function retry<TResponse>(options: RetryOptions,func: (exit: (err: any) => void) => Promise<TResponse>,): Promise<TResponse> {const times = options?.times ?? 3;const delay = options?.delay;const backoff = options?.backoff ?? null;let i = 0;while (true) {const [err, result] = (await tryit(func)((err: any) => { throw { _exited: err };})) as [any, TResponse];if (!err) { return result;}if (err._exited) { throw err._exited;}if (++i >= times) { throw err;}if (delay) { await sleep(delay);}if (backoff) { await sleep(backoff(i));}}}