Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The documentation of onFailedAttempt(error) is incorrect and misleading. #65

Open
issuefiler opened this issue May 29, 2022 · 0 comments

Comments

@issuefiler
Copy link

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:

  1. The 1st attempt
  2. onFailedAttempt({attemptNumber: 1, retriesLeft: 5})
  3. The 2nd attempt (the 1st retry)
  4. onFailedAttempt({attemptNumber: 2, retriesLeft: 4})
  5. The 6th attempt (the 5th retry)
  6. onFailedAttempt({attemptNumber: 6, retriesLeft: 0})
@issuefiler issuefiler changed the title The documentation of onFailedAttempt(error) is incorrect. The documentation of onFailedAttempt(error) is incorrect and misleading. May 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant