summaryrefslogtreecommitdiffstats
path: root/src/utils.ts
blob: b9b5cfafcff3c6e4790fe6be9e687e16e0f82f7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
export const wait = (ms: number) =>
  new Promise((resolve) => setTimeout(resolve, ms));

export const runWithRetries = async <T>(
  fn: () => Promise<T>,
  retries: number,
  backoff: number = 2000
): Promise<T> => {
  let attempts = 0;
  while (true) {
    await wait(attempts * backoff);
    try {
      return await fn();
    } catch (e) {
      if (attempts >= retries) {
        throw e;
      }

      console.error("Error:", e);
      attempts++;
    }
  }
};