diff options
author | Alexander Early <alexander.early@gmail.com> | 2015-06-17 11:44:51 -0700 |
---|---|---|
committer | Alexander Early <alexander.early@gmail.com> | 2015-06-17 11:44:51 -0700 |
commit | bd8325fb5b07b296733f1638fa8dcd7b8fc684cd (patch) | |
tree | 261125208ec9ad1851f535836526f486cb6bf0f9 /README.md | |
parent | 0eb41fa8c8d4be260883ac4b73a7ed7c6ed69f8e (diff) | |
parent | 33b06ac7418eafed4c15c7202a7c93a65325dd31 (diff) | |
download | async-bd8325fb5b07b296733f1638fa8dcd7b8fc684cd.tar.gz |
Merge pull request #793 from ahum/feature/retry-add-optional-interval
Feature/retry add optional interval
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 23 |
1 files changed, 15 insertions, 8 deletions
@@ -62,12 +62,12 @@ This can also arise by accident if you callback early in certain cases: ```js async.eachSeries(hugeArray, function iterator(item, callback) { if (inCache(item)) { - callback(null, cache[item]); // if many items are cached, you'll overflow + callback(null, cache[item]); // if many items are cached, you'll overflow } else { doSomeIO(item, callback); } -}, function done() { - //... +}, function done() { + //... }); ``` @@ -1459,7 +1459,7 @@ new tasks much easier (and the code more readable). --------------------------------------- <a name="retry" /> -### retry([times = 5], task, [callback]) +### retry([opts = {times: 5, interval: 0}| 5], task, [callback]) Attempts to get a successful response from `task` no more than `times` times before returning an error. If the task is successful, the `callback` will be passed the result @@ -1468,7 +1468,8 @@ result (if any) of the final attempt. __Arguments__ -* `times` - An integer indicating how many times to attempt the `task` before giving up. Defaults to 5. +* `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). * `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 @@ -1485,6 +1486,12 @@ async.retry(3, apiMethod, function(err, result) { }); ``` +```js +async.retry({times: 3, interval: 200}, apiMethod, function(err, result) { + // do something with the result +}); +``` + It can also be embeded within other control flow functions to retry individual methods that are not as reliable, like this: @@ -1646,7 +1653,7 @@ async.times(5, function(n, next){ <a name="timesSeries" /> ### timesSeries(n, iterator, [callback]) -The same as [`times`](#times), only the iterator is applied in series. +The same as [`times`](#times), only the iterator is applied in series. The next `iterator` is only called once the current one has completed. The results array will be in the same order as the original. @@ -1727,9 +1734,9 @@ function sometimesAsync(arg, callback) { } // this has a risk of stack overflows if many results are cached in a row -async.mapSeries(args, sometimesAsync, done); +async.mapSeries(args, sometimesAsync, done); -// this will defer sometimesAsync's callback if necessary, +// this will defer sometimesAsync's callback if necessary, // preventing stack overflows async.mapSeries(args, async.ensureAsync(sometimesAsync), done); |