export const retryAll = async <T>(
  promises: (() => Promise<T>)[],
  retries: number = 3,
  delay: number = 1000
) => {
  const wrappedPromises = promises.map((fn) => retry(fn, retries, delay))
  return Promise.allSettled(wrappedPromises)
}

export const retry = async <T>(
  fn: () => Promise<T>,
  retries: number = 3,
  delay: number = 1000
): Promise<T> => {
  try {
    return await fn()
  } catch (err) {
    if (retries === 0) {
      return Promise.reject(err)
    }
    return new Promise((resolve) =>
      setTimeout(() => resolve(retry(fn, retries - 1)), delay)
    )
  }
}
