Code Quiz

Retry Wrapper Reliability Bug

A retry helper for a flaky dependency hides failures instead of surfacing them, undermining fault tolerance.

Codejavascript
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

async function callWithRetry(fn, maxRetries = 5) {
  let attempt = 0;
  let lastError;
  while (attempt < maxRetries) {
    try {
      return await fn();
    } catch (err) {
      lastError = err;
      attempt++;
      const delay = 100 * Math.pow(2, attempt) + Math.random() * 100;
      await sleep(delay);
    }
  }
}

// Usage in an order service
async function chargePayment(order) {
  const receipt = await callWithRetry(() => paymentGateway.charge(order));
  return { status: 'PAID', receipt };
}

What is the reliability bug in this retry wrapper as used by chargePayment?