All files / src/shared/lib/helpers promise.ts

90.9% Statements 10/11
75% Branches 6/8
66.66% Functions 2/3
88.88% Lines 8/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25            9x       15x 22x 22x   11x 4x     7x 7x          
type RetryOptions = {
  retries?: number;
  delayMs?: number;
  shouldRetry?: (error: unknown) => boolean;
};
 
export const promiseRetry = async <T>(
  operation: () => Promise<T>,
  { retries = 5, delayMs = 50, shouldRetry = () => false }: RetryOptions = {},
): Promise<T> => {
  for (let attempt = 0; ; attempt++) {
    try {
      return await operation();
    } catch (error) {
      if (attempt >= retries || !shouldRetry(error)) {
        throw error;
      }
 
      await new Promise<void>((resolve) =>
        setTimeout(resolve, delayMs * 2 ** attempt),
      );
    }
  }
};