mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
39 lines
973 B
TypeScript
39 lines
973 B
TypeScript
export interface RetryOptions {
|
|
times?: number;
|
|
delay?: number;
|
|
backoff: (options: RetryOptions & { count: number }) => number;
|
|
}
|
|
|
|
/**
|
|
* @name retry
|
|
* @category Async
|
|
* @description Retries a function a specified number of times with a delay between each retry
|
|
*
|
|
* @param {Promise<unknown>} fn - The function to retry
|
|
* @param {RetryOptions} options - The options for the retry
|
|
* @returns {Promise<unknown>} - The result of the function
|
|
*
|
|
* @example
|
|
* const result = await retry(() => {
|
|
* return fetch('https://jsonplaceholder.typicode.com/todos/1')
|
|
* .then(response => response.json())
|
|
* });
|
|
*
|
|
* @example
|
|
* const result = await retry(() => {
|
|
* return fetch('https://jsonplaceholder.typicode.com/todos/1')
|
|
* .then(response => response.json())
|
|
* }, { times: 3, delay: 1000 });
|
|
*
|
|
*/
|
|
export async function retry<Return>(
|
|
fn: () => Promise<Return>,
|
|
options: RetryOptions
|
|
) {
|
|
const {
|
|
times = 3,
|
|
} = options;
|
|
|
|
let count = 0;
|
|
}
|