Skip to content

Commit 972fb25

Browse files
committed
Improve docs for calculateDelay option
Fixes #2415
1 parent 2d6359a commit 972fb25

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

documentation/7-retry.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ interface RetryObject {
127127
The function used to calculate the delay before the next request is made. Returning `0` cancels the retry.
128128

129129
**Note:**
130-
> - This function is responsible for the entire retry mechanism, including the `limit` property. To support this, you need to check if `computedValue` is different than `0`.
130+
> - When you provide this function, you take full control of the retry logic. The `limit` option is not automatically enforced - it's your responsibility to check `attemptCount` or respect when `computedValue` is `0`.
131+
> - The `computedValue` parameter contains the default retry delay calculation, which is `0` when the limit is exceeded or the error is not retryable. To maintain default retry behavior while customizing delays, check if `computedValue === 0` and return `0` to stop retrying.
131132
132133
**Tip:**
133134
> - This is especially useful when you want to scale down the computed value.
@@ -137,7 +138,15 @@ import got from 'got';
137138

138139
await got('https://httpbin.org/anything', {
139140
retry: {
141+
limit: 3,
140142
calculateDelay: ({computedValue}) => {
143+
// When computedValue is 0, the default logic says don't retry
144+
// (limit exceeded, non-retryable error, etc.)
145+
if (computedValue === 0) {
146+
return 0;
147+
}
148+
149+
// Scale down the delay
141150
return computedValue / 10;
142151
}
143152
}

source/core/options.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.ra
388388
The `calculateDelay` property is a `function` that receives an object with `attemptCount`, `retryOptions`, `error` and `computedValue` properties for current retry count, the retry options, error and default computed value.
389389
The function must return a delay in milliseconds (or a Promise resolving with it) (`0` return value cancels retry).
390390
391+
__Note:__ When you provide `calculateDelay`, you take full control of retry decisions. The `limit` option is not automatically enforced - you must check `attemptCount` yourself or return `0` when `computedValue` is `0` to respect the default retry logic.
392+
391393
By default, it retries *only* on the specified methods, status codes, and on these network errors:
392394
- `ETIMEDOUT`: One of the [timeout](#timeout) limits were reached.
393395
- `ECONNRESET`: Connection was forcibly closed by a peer.
@@ -2080,6 +2082,8 @@ export default class Options {
20802082
The `calculateDelay` property is a `function` that receives an object with `attemptCount`, `retryOptions`, `error` and `computedValue` properties for current retry count, the retry options, error and default computed value.
20812083
The function must return a delay in milliseconds (or a Promise resolving with it) (`0` return value cancels retry).
20822084
2085+
__Note:__ When you provide `calculateDelay`, you take full control of retry decisions. The `limit` option is not automatically enforced - you must check `attemptCount` yourself or return `0` when `computedValue` is `0` to respect the default retry logic.
2086+
20832087
By default, it retries *only* on the specified methods, status codes, and on these network errors:
20842088
20852089
- `ETIMEDOUT`: One of the [timeout](#timeout) limits were reached.

0 commit comments

Comments
 (0)