es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
15 lines (14 loc) • 709 B
TypeScript
/**
* Retries a function that returns a promise until it succeeds or the maximum number of retries is reached.
* @param {() => Promise<T>} fn - The function to retry.
* @param {number} retries - The maximum number of retries.
* @returns {Promise<T>} A promise that resolves with the function's result or rejects if all retries fail.
* @template T
* @example
* const unreliableFunction = () => {
* return Math.random() > 0.7 ? Promise.resolve('Success') : Promise.reject('Failed');
* };
* const result = await retry(unreliableFunction, 5);
* console.log(result); // 'Success' (if it succeeds within 5 tries)
*/
export declare function retry<T>(fn: () => Promise<T>, retries: number): Promise<T>;