-
Notifications
You must be signed in to change notification settings - Fork 27
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
feat(async module): added onRetry option for retry fn #229
base: main
Are you sure you want to change the base?
Conversation
ccbc741
to
d8ce877
Compare
@gperdomor Thank you for the PR! The proposed changes significantly improve the function. However, I’ve left a few comments for discussion and adjustments before we can proceed |
Hey @gperdomor, thanks for your contribution 👍 I plan to review this after I get v12.2.0 out (just gotta finish the release notes, probably next week). |
export type RetryOptions = { | ||
times?: number | ||
delay?: number | null | ||
onRetry?: (err: Error, attemptNumber: number) => void | ||
backoff?: (count: number) => number | ||
} | ||
|
||
/** | ||
* Retries the given function the specified number of times. | ||
* | ||
* @param {Object} options - The employee who is responsible for the project. | ||
* @param {number} [options.times=3] - Number of attempts. | ||
* @param {number} [options.delay] - Specify milliseconds to sleep between attempts. | ||
* @param {Function} [options.onRetry=null] - Is invoked after a new retry is performed. It's passed the Error that triggered it as a parameter and the attempt number. | ||
* @param {Function} [options.backoff=null] - The backoff option is like delay but uses a function to sleep -- makes for easy exponential backoff. | ||
* @param {Function} func - Function to be executed | ||
* | ||
* @see https://radashi.js.org/reference/async/retry | ||
* @example | ||
* ```ts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I said to use Jsoc, I meant to use it in the type, because using it only in the function would not have a good integration with VSCode.
export type RetryOptions = { | |
times?: number | |
delay?: number | null | |
onRetry?: (err: Error, attemptNumber: number) => void | |
backoff?: (count: number) => number | |
} | |
/** | |
* Retries the given function the specified number of times. | |
* | |
* @param {Object} options - The employee who is responsible for the project. | |
* @param {number} [options.times=3] - Number of attempts. | |
* @param {number} [options.delay] - Specify milliseconds to sleep between attempts. | |
* @param {Function} [options.onRetry=null] - Is invoked after a new retry is performed. It's passed the Error that triggered it as a parameter and the attempt number. | |
* @param {Function} [options.backoff=null] - The backoff option is like delay but uses a function to sleep -- makes for easy exponential backoff. | |
* @param {Function} func - Function to be executed | |
* | |
* @see https://radashi.js.org/reference/async/retry | |
* @example | |
* ```ts | |
export type RetryOptions = { | |
/** | |
* Number of attempts. | |
* | |
* @default 3 | |
*/ | |
times?: number | |
/** Specify milliseconds to sleep between attempts */ | |
delay?: number | null | |
/** | |
* Is invoked after a new retry is performed. It's passed the Error that triggered it as a parameter and the attempt number. | |
* | |
* @default null | |
*/ | |
onRetry?: (err: Error, attemptNumber: number) => void | |
/** | |
* The backoff option is like delay but uses a function to sleep -- makes for easy exponential backoff. | |
* | |
* @default null | |
*/ | |
backoff?: (count: number) => number | |
} | |
/** | |
* Retries the given function the specified number of times. | |
* | |
* @see https://radashi.js.org/reference/async/retry | |
* @example | |
* ```ts |
Thanks for this PR, @gperdomor! It's great to have you here. If you'd like to address Marlon's comments, go right ahead. Otherwise, I'll get around to it at some point (after I get the v12.2.0 out, of course). Two questions I had:
Your feedback would be appreciated. |
Ping @gperdomor: It'd be great to get your perspective on #229 (comment) |
Closing due to lack of response. Can reopen when you have more time. 👍 |
Hi @aleclarson, I didn't saw this, sorry...
In my case, I want to log...
Mmmmm... I don't know, I think could be useful fro request calls, for example maybe a should not retry if status code is 401 or 403 because in all retries we should get the same result, bt we can retry if status is something like 408 (Request Timeout) |
7f5694c
to
5483a64
Compare
@aleclarson for the second topic, what do you think it's a better option:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @gperdomor, I've had time to think about this a bit.
- Recently, we've decided to align our APIs to popular, full-featured, standalone packages. (We don't want to clone them in their entirety, just mimic them to make migrating to them easier if you ever need more than Radashi provides.) In this case, that's the
p-retry
package. - Therefore, this option should align with onFailedAttempt from
p-retry
. That includes naming, the function signature, and when it gets called. - Finally, with the addition of a
signal
option in feat: addsignal
option toretry
andparallel
#262, we'll have a means of aborting the retry loop, so this PR can stay focused on theonFailedAttempt
feature.
Any questions, just ask. 👍
Ping @gperdomor, let me know if you'll have time for this PR. 👍 Hey! There's a new requirement for PRs that introduce new features. Without this requirement met, we won't be able to merge this. Note that this PR can still be included in a
|
Tip
The owner of this PR can publish a preview release by commenting
/publish
in this PR. Afterwards, anyone can try it out by runningpnpm add radashi@pr<PR_NUMBER>
.Summary
Related issue, if any:
For any code change,
Does this PR introduce a breaking change?
No
Bundle impact
src/async/retry.ts
Footnotes
Function size includes the
import
dependencies of the function. ↩