es-promise-ext
Version:
Native promise extensions for javascript and typescript.
23 lines (22 loc) • 1 kB
TypeScript
export type AsyncFunction<T> = () => Promise<T>;
export type RetryFunction<T> = () => Promise<T>;
/**
* Starts a promise with an asynchronous function that has retry tolerance.
*
* @param {AsyncFunction<T> | Promise<T>} asyncFunction
* - An asynchronous function that will be called, returning a result in a promise.
* @param {number} [count=3]
* - A positive integer between 0 and 30 indicating the number of retries.
* @param {number} [delay=100]
* - The time in milliseconds to wait between retries.
*
* @return {RetryFunction<T>}
* The function wrapper of return the result within a promise after retries.
*
* @example
* Promise.resolve().then(retry(asyncFunction, 5, 1000));
* Promise.resolve().then(retry(() => asyncFunction(1, 2), 5, 1000));
* Promise.resolve().then(retry(promise, 5, 1000));
* // Retry 5 times with a 1-second interval.
*/
export default function retry<T>(asyncFunction: AsyncFunction<T> | PromiseLike<T>, count?: number, delay?: number): AsyncFunction<T>;