UNPKG

@augment-vir/common

Version:

A collection of augments, helpers types, functions, and classes for any JavaScript environment.

52 lines (51 loc) 1.79 kB
import { type PartialWithUndefined } from '@augment-vir/common'; import { type AtLeastOneDuration } from '@date-vir/duration'; import { type IsEqual } from 'type-fest'; /** * Params for the callback passed to {@link retry}. * * @category Internal * @category Package : @augment-vir/common * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export type RetryCallbackParams = { /** This will be `0` for the first execution, then increment with each retry. */ retryCount: number; /** Only true on the last retry. */ isLastRetry: boolean; /** Only true on the first execution, before any retries. */ isFirstExecution: boolean; /** Only true on the first retry. */ isFirstRetry: boolean; }; /** * Calls `callback` until it doesn't throw an error or throws an error when `maxRetries` is reached. * Similar to the `waitUntil` guard from '@augment-vir/assert' but doesn't check the callback's * output. * * @category Function * @category Package : @augment-vir/common * @example * * ```ts * import {callWithRetries} from '@augment-vir/common'; * * const result = callWithRetries(5, () => { * if (Math.random() < 0.5) { * return 'done'; * } else { * throw new Error(); * } * }); * ``` * * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export declare function retry<const T, const Duration extends AtLeastOneDuration | undefined = undefined>(maxRetries: number, callback: (params: RetryCallbackParams) => T, options?: PartialWithUndefined<{ /** * Wait this duration between each retry. * * @default {seconds: 1} */ interval: Duration; }>): IsEqual<Duration, undefined> extends true ? T : Promise<Awaited<T>>;