Skip to content

Commit

Permalink
Add docs for retry interval function
Browse files Browse the repository at this point in the history
  • Loading branch information
aianus committed May 19, 2016
1 parent aed9bc1 commit 22d13bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,7 @@ __Arguments__
* `opts` - Can be either an object with `times` and `interval` or a number.
* `times` - The number of attempts to make before giving up. The default is `5`.
* `interval` - The time to wait between retries, in milliseconds. The default is `0`.
The interval may also be specified as a function of the retry count.
* If `opts` is a number, the number specifies the number of times to retry, with the default interval of `0`.
* `task(callback, results)` - A function which receives two arguments: (1) a `callback(err, result)`
which must be called when finished, passing `err` (which can be `null`) and the `result` of
Expand All @@ -1595,6 +1596,21 @@ async.retry({times: 3, interval: 200}, apiMethod, function(err, result) {
});
```

The interval may also be specified as a function of the retry count:

```js
// try calling apiMethod 10 times with exponential backoff
// (i.e. intervals of 100, 200, 400, 800, 1600, ... milliseconds)
async.retry({
times: 10,
interval: function(retryCount) {
return 50 * Math.pow(2, retryCount);
}
}, apiMethod, function(err, result) {
// do something with the result
});
```

```js
// try calling apiMethod the default 5 times no delay between each retry
async.retry(apiMethod, function(err, result) {
Expand Down
16 changes: 14 additions & 2 deletions lib/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import constant from 'lodash/constant';
* * `times` - The number of attempts to make before giving up. The default
* is `5`.
* * `interval` - The time to wait between retries, in milliseconds. The
* default is `0`.
* default is `0`. The interval may also be specified as a function of the
* retry count.
* * If `opts` is a number, the number specifies the number of times to retry,
* with the default interval of `0`.
* @param {Function} task - A function which receives two arguments: (1) a
Expand Down Expand Up @@ -47,7 +48,18 @@ import constant from 'lodash/constant';
* // do something with the result
* });
*
* // try calling apiMethod the default 5 times no delay between each retry
* // try calling apiMethod 10 times with exponential backoff
* // (i.e. intervals of 100, 200, 400, 800, 1600, ... milliseconds)
* async.retry({
* times: 10,
* interval: function(retryCount) {
* return 50 * Math.pow(2, retryCount);
* }
* }, apiMethod, function(err, result) {
* // do something with the result
* });
*
* // try calling apiMethod the default 5 times no delay between each retry
* async.retry(apiMethod, function(err, result) {
* // do something with the result
* });
Expand Down

0 comments on commit 22d13bf

Please sign in to comment.