UNPKG

@utilify/core

Version:

Modern, strongly typed, and safe utility function library for JavaScript and TypeScript. Includes type checking, manipulation of arrays, objects, strings, dates, colors, numbers, regular expressions, and more. Compatible with Browser, Node.js, Deno, and B

41 lines 1.53 kB
type BackoffMode = 'fixed' | 'linear' | 'exponential'; type JitterMode = 'none' | 'full' | 'equal' | 'decorrelated'; interface BackoffOptions { backoffMode?: BackoffMode; initialDelay?: number; jitterMode?: JitterMode; maxDelay?: number; maxAttempts?: number; onRetry?: (attempt: number, currentDelay: number) => void; } /** * @callback BackoffAction * @returns {Promise<any>} The promise to execute. */ /** * @callback BackoffOnRetry * @param {number} attempt - The current attempt number. * @param {number} currentDelay - The current delay before retry. * @returns {void} */ /** * Options for backoff. * @typedef {Object} BackoffOptions * @property {'fixed'|'linear'|'exponential'} [backoffMode="exponential"] * @property {number} [initialDelay=500] * @property {'none'|'full'|'equal'|'decorrelated'} [jitterMode="full"] * @property {number} [maxDelay=60000] * @property {number} [maxAttempts=5] * @property {BackoffOnRetry} [onRetry] */ /** * Executes an async action with retry and backoff strategies. * @template T * @param {BackoffAction} action - The async function to execute. * @param {BackoffOptions} [options] - Backoff configuration. * @returns {Promise<T>} The result of the action. * @throws {Error} If parameters are invalid or retries exhausted. */ export default function backoff<T>(action: () => Promise<T>, { initialDelay, backoffMode, jitterMode, maxAttempts, maxDelay, onRetry, }?: BackoffOptions): Promise<T>; export {}; //# sourceMappingURL=backoff.d.ts.map