summaryrefslogtreecommitdiff
path: root/lib/retry.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/retry.js')
-rw-r--r--lib/retry.js44
1 files changed, 10 insertions, 34 deletions
diff --git a/lib/retry.js b/lib/retry.js
index 5c22af3..bf0dcf1 100644
--- a/lib/retry.js
+++ b/lib/retry.js
@@ -1,4 +1,3 @@
-import series from './series';
import noop from 'lodash/noop';
import constant from 'lodash/constant';
@@ -104,43 +103,20 @@ export default function retry(opts, task, callback) {
callback = callback || noop;
}
-
if (typeof task !== 'function') {
throw new Error("Invalid arguments for async.retry");
}
- var attempts = [];
- for (var i = 1; i < options.times + 1; i++) {
- var isFinalAttempt = (i == options.times);
- attempts.push(retryAttempt(isFinalAttempt));
- var interval = options.intervalFunc(i);
- if (!isFinalAttempt && interval > 0) {
- attempts.push(retryInterval(interval));
- }
+ var attempt = 1;
+ function retryAttempt() {
+ task(function(err, result) {
+ if (err && attempt++ < options.times) {
+ setTimeout(retryAttempt, options.intervalFunc(attempt));
+ } else {
+ callback(err, result);
+ }
+ });
}
- series(attempts, function(done, data) {
- data = data[data.length - 1];
- callback(data.err, data.result);
- });
-
-
- function retryAttempt(isFinalAttempt) {
- return function(seriesCallback) {
- task(function(err, result) {
- seriesCallback(!err || isFinalAttempt, {
- err: err,
- result: result
- });
- });
- };
- }
-
- function retryInterval(interval) {
- return function(seriesCallback) {
- setTimeout(function() {
- seriesCallback(null);
- }, interval);
- };
- }
+ retryAttempt();
}