fix: off-by-one in retryOn — retries: 0 still retries once#14
Conversation
The retryOn callback used `attempt > retries`, but fetch-retry starts counting at attempt=0. With the default `retries: 0`, the check `0 > 0` is false, so the SDK enters the retry logic and retries once. Change to `attempt >= retries` so that `retries: 0` means zero retries, matching the README which states "retries are not active by default". Add parameterized tests verifying exact fetch call counts for retries: 0, 1, and 2. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
hey @AnderRV, is this repo still being maintained? We are customers, and we are facing this bug. We workaround it setting |
Hi @jorgehermo9, thanks for the thorough write-up and the fix. Yes, the repo is maintained, apologies that Issues were turned off, we'll re-enable them. Thanks again for the contribution! |
|
@jorgehermo9 quick heads up: your fix is live on npm as Thanks again, much appreciated 🙏 |
Closes #15
Bug
The
retryOncallback in the constructor usesattempt > retriesto decide whether to stop retrying. However,fetch-retrystarts counting attempts at 0, so with the defaultretries: 0:attempt=0:0 > 0→false→ enters retry logic → retries onceattempt=1:1 > 0→true→ stopsThis means
retries: 0(the default) actually performs 1 retry, making 2 fetch calls total. The README states "retries are not active by default", which contradicts this behavior.Fix
Change
attempt > retriestoattempt >= retriesin theretryOncallback (one character change insrc/index.ts).With
>=,retries: 0now correctly means zero retries:attempt=0:0 >= 0→true→ stops immediately → no retryTests
Added parameterized tests (
tests/retry-count.test.ts) verifying exact fetch call counts:retriesAll tests pass with the fix and fail without it.