From f8cea478196657170e829c4205b88ed3ef36cc09 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Thu, 7 Jan 2016 15:05:37 -0800 Subject: clarify retry docs. Closes #936 --- README.md | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d5d1b7e..b9f20e3 100644 --- a/README.md +++ b/README.md @@ -1445,8 +1445,10 @@ result (if any) of the final attempt. __Arguments__ -* `opts` - Can be either an object with `times` and `interval` or a number. `times` is how many attempts should be made before giving up. `interval` is how long to wait inbetween attempts. Defaults to {times: 5, interval: 0} - * if a number is passed in it sets `times` only (with `interval` defaulting to 0). +* `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`. + * 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 the function's execution, and (2) a `results` object, containing the results of @@ -1454,21 +1456,29 @@ __Arguments__ * `callback(err, results)` - An optional callback which is called when the task has succeeded, or after the final failed attempt. It receives the `err` and `result` arguments of the last attempt at completing the `task`. -The [`retry`](#retry) function can be used as a stand-alone control flow by passing a -callback, as shown below: +The [`retry`](#retry) function can be used as a stand-alone control flow by passing a callback, as shown below: ```js +// try calling apiMethod 3 times async.retry(3, apiMethod, function(err, result) { // do something with the result }); ``` ```js +// try calling apiMethod 3 times, waiting 200 ms between each retry async.retry({times: 3, interval: 200}, 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) { + // do something with the result +}); +``` + It can also be embedded within other control flow functions to retry individual methods that are not as reliable, like this: -- cgit v1.2.1