Skip to content

Retrying made easy

We can write a simple function to retry our jobs until they pass or reach our limit

ts
import { job, task } from "@pandaci/core";

async function retryPromise<T>(
  fn: () => Promise<T>,
  retries: number,
): Promise<T> {
  while (retries > 0) {
    try {
      return await fn(); // Try executing the function
    } catch (err) {
      retries--; // Decrease retry count if it fails
      if (retries === 0) throw err; // Throw error if out of retries
    }
  }
}

retryPromise(
  job("Might fail", () => {}),
  3,
);