es-toolkit icon indicating copy to clipboard operation
es-toolkit copied to clipboard

retry function retries one time less than specified

Open uwemaurer opened this issue 6 months ago • 2 comments

in the retry function there is a retries option, but it is actually the number of attempts, not retries.

for example this code

import { delay, retry } from 'es-toolkit';

let request  = 0;

async function f() {
    console.log("request", request++);
    await delay(10)
    throw Error("err!")
}

await retry(f, 1);

outputs

request 0

Error: err!

so it only does one request, zero retries instead of one.

the problem is this line in retry

for (let attempts = 0; attempts < retries; attempts++) {

should be

for (let attempts = 0; attempts <= retries; attempts++) {

then the function also works when calling it with 0 retries (which currently does not work)

uwemaurer avatar Jun 13 '25 07:06 uwemaurer