Skip to content

Commit 22d13bf

Browse files
committed
Add docs for retry interval function
1 parent aed9bc1 commit 22d13bf

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,7 @@ __Arguments__
15711571
* `opts` - Can be either an object with `times` and `interval` or a number.
15721572
* `times` - The number of attempts to make before giving up. The default is `5`.
15731573
* `interval` - The time to wait between retries, in milliseconds. The default is `0`.
1574+
The interval may also be specified as a function of the retry count.
15741575
* If `opts` is a number, the number specifies the number of times to retry, with the default interval of `0`.
15751576
* `task(callback, results)` - A function which receives two arguments: (1) a `callback(err, result)`
15761577
which must be called when finished, passing `err` (which can be `null`) and the `result` of
@@ -1595,6 +1596,21 @@ async.retry({times: 3, interval: 200}, apiMethod, function(err, result) {
15951596
});
15961597
```
15971598

1599+
The interval may also be specified as a function of the retry count:
1600+
1601+
```js
1602+
// try calling apiMethod 10 times with exponential backoff
1603+
// (i.e. intervals of 100, 200, 400, 800, 1600, ... milliseconds)
1604+
async.retry({
1605+
times: 10,
1606+
interval: function(retryCount) {
1607+
return 50 * Math.pow(2, retryCount);
1608+
}
1609+
}, apiMethod, function(err, result) {
1610+
// do something with the result
1611+
});
1612+
```
1613+
15981614
```js
15991615
// try calling apiMethod the default 5 times no delay between each retry
16001616
async.retry(apiMethod, function(err, result) {

lib/retry.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import constant from 'lodash/constant';
1919
* * `times` - The number of attempts to make before giving up. The default
2020
* is `5`.
2121
* * `interval` - The time to wait between retries, in milliseconds. The
22-
* default is `0`.
22+
* default is `0`. The interval may also be specified as a function of the
23+
* retry count.
2324
* * If `opts` is a number, the number specifies the number of times to retry,
2425
* with the default interval of `0`.
2526
* @param {Function} task - A function which receives two arguments: (1) a
@@ -47,7 +48,18 @@ import constant from 'lodash/constant';
4748
* // do something with the result
4849
* });
4950
*
50-
* // try calling apiMethod the default 5 times no delay between each retry
51+
* // try calling apiMethod 10 times with exponential backoff
52+
* // (i.e. intervals of 100, 200, 400, 800, 1600, ... milliseconds)
53+
* async.retry({
54+
* times: 10,
55+
* interval: function(retryCount) {
56+
* return 50 * Math.pow(2, retryCount);
57+
* }
58+
* }, apiMethod, function(err, result) {
59+
* // do something with the result
60+
* });
61+
*
62+
* // try calling apiMethod the default 5 times no delay between each retry
5163
* async.retry(apiMethod, function(err, result) {
5264
* // do something with the result
5365
* });

0 commit comments

Comments
 (0)