All files / core/src/shared retry.ts

100% Statements 19/19
100% Branches 5/5
100% Functions 1/1
100% Lines 19/19

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 233x 3x 3x 3x 3x 1x 1x   2x 2x   3x 4x 4x 4x 4x 4x 4x 4x   2x 2x  
export async function retry<T>(
  fn: () => Promise<T>,
  { maxRetries = 0 }: { maxRetries?: number } = {},
): Promise<T> {
  if (maxRetries < 0) {
    throw new TypeError('maxRetries must be greater than 0');
  }
 
  let attempts = 0;
  let lastError: unknown | undefined;
 
  while (attempts < maxRetries + 1) {
    try {
      return await fn();
    } catch (error) {
      attempts++;
      lastError = error;
    }
  }
 
  throw lastError;
}