This is the example code on the readme.md.
import pRetry from 'p-retry';
const run = async () => {
const response = await fetch('https://sindresorhus.com/unicorn');
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
};
const result = await pRetry(run, {
onFailedAttempt: error => {
console.log(`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`);
// 1st request => Attempt 1 failed. There are 4 retries left.
// 2nd request => Attempt 2 failed. There are 3 retries left.
// …
},
retries: 5
});
console.log(result);
The comments say
// 1st request => Attempt 1 failed. There are 4 retries left.
// 2nd request => Attempt 2 failed. There are 3 retries left.
But this is neither true nor the actual output. When the 1st attempt fails, there are still 5 retries left, because the retries
does not count the first try as a re-try.
Fortunately this issue is not a bug of the code, but a mistake on the documentation.
The actual output for the retries
of 5
is:
onFailedAttempt({attemptNumber: 1, retriesLeft: 5})
onFailedAttempt({attemptNumber: 2, retriesLeft: 4})
onFailedAttempt({attemptNumber: 6, retriesLeft: 0})
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too