retry

Executes an asynchronous function and retries it if it fails. This function is particularly useful for handling network requests or other potentially unreliable operations.
> retry<T>(fn: () => Promise<T>, retries: number = 3): Promise<T>
Parameters
  • fn: The asynchronous function to execute.
  • retries (optional): The number of retry attempts if `fn` fails. Default is `3`.
  • Returns: A `Promise` that resolves with the result of `fn` or rejects if all retries fail.

Example

import { retry } from "@explita/daily-toolset";

async function fetchData() {
    // Some asynchronous operation, e.g., API 		request
}
    
retry(fetchData, 3).then((result) => console.log(result)).catch(console.error);