diff options
-rw-r--r-- | README.md | 17 | ||||
-rw-r--r-- | lib/retry.js | 16 |
2 files changed, 30 insertions, 3 deletions
@@ -1570,7 +1570,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`. + * `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 (see example). * 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 @@ -1595,6 +1595,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) { diff --git a/lib/retry.js b/lib/retry.js index 99040e8..633b418 100644 --- a/lib/retry.js +++ b/lib/retry.js @@ -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 (see example). * * 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 @@ -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 * }); |