diff options
author | Bojan Djurkovic <dbojan@gmail.com> | 2016-08-04 21:00:56 -0300 |
---|---|---|
committer | Bojan Djurkovic <dbojan@gmail.com> | 2016-08-04 21:02:40 -0300 |
commit | 0af976cc89096ee64db3bd08fe65e2b3445f56d5 (patch) | |
tree | c945983223c8e909d4add41dca28295737d200c7 | |
parent | 0337ee0b2e582917cc15e6efc186e3c89e0a2cdb (diff) | |
download | async-0af976cc89096ee64db3bd08fe65e2b3445f56d5.tar.gz |
rename the new retry option to errorFilter and consolidate retry attempt condition into one statement
-rw-r--r-- | lib/retry.js | 26 | ||||
-rw-r--r-- | mocha_test/retry.js | 8 | ||||
-rw-r--r-- | mocha_test/retryable.js | 2 |
3 files changed, 16 insertions, 20 deletions
diff --git a/lib/retry.js b/lib/retry.js index d8b711b..45b09a8 100644 --- a/lib/retry.js +++ b/lib/retry.js @@ -19,11 +19,11 @@ import constant from 'lodash/constant'; * * `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). - * * `continueOperation` - An optional synchronous function that is invoked on - * erroneous result with the the error. If it returns `true` the retry attempts - * will continue, if the function returns `false` the retry flow is aborted - * with the current attempt's error and result being returned to the final - * callback. Invoked with (err). + * * `errorFilter` - An optional synchronous function that is invoked on + * erroneous result. If it returns `true` the retry attempts will continue; + * if the function returns `false` the retry flow is aborted with the current + * attempt's error and result being returned to the final callback. + * Invoked with (err, result). * * 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 @@ -70,7 +70,7 @@ import constant from 'lodash/constant'; * // try calling apiMethod only when error condition satisfies, all other * // errors will abort the retry control flow and return to final callback * async.retry({ - * continueOperation: function(err) { + * errorFilter: function(err) { * return err.message === 'Temporary error'; // only retry on a specific error * } * }, apiMethod, function(err, result) { @@ -104,7 +104,7 @@ export default function retry(opts, task, callback) { t.interval : constant(+t.interval || DEFAULT_INTERVAL); - acc.continueOperation = t.continueOperation; + acc.errorFilter = t.errorFilter; } else if (typeof t === 'number' || typeof t === 'string') { acc.times = +t || DEFAULT_TIMES; } else { @@ -127,14 +127,10 @@ export default function retry(opts, task, callback) { var attempt = 1; function retryAttempt() { task(function(err) { - if (err && attempt++ < options.times) { - var proceed = typeof options.continueOperation != 'function' || - options.continueOperation(err); - if(proceed) { - setTimeout(retryAttempt, options.intervalFunc(attempt)); - } else { - callback.apply(null, arguments); - } + if (err && attempt++ < options.times && + (typeof options.errorFilter != 'function' || + options.errorFilter(err))) { + setTimeout(retryAttempt, options.intervalFunc(attempt)); } else { callback.apply(null, arguments); } diff --git a/mocha_test/retry.js b/mocha_test/retry.js index 612b144..757b7ab 100644 --- a/mocha_test/retry.js +++ b/mocha_test/retry.js @@ -173,7 +173,7 @@ describe("retry", function () { } var options = { times: times, - continueOperation: errorTest + errorFilter: errorTest }; async.retry(options, fn, function(err, result){ assert.equal(callCount, 3, "did not retry the correct number of times"); @@ -197,7 +197,7 @@ describe("retry", function () { return err && err === error + callCount; // just a different pattern } var options = { - continueOperation: errorTest + errorFilter: errorTest }; async.retry(options, fn, function(err, result){ assert.equal(callCount, 2, "did not retry the correct number of times"); @@ -223,7 +223,7 @@ describe("retry", function () { return err && err !== special; } var start = new Date().getTime(); - async.retry({ interval: interval, continueOperation: errorTest }, fn, function(err, result){ + async.retry({ interval: interval, errorFilter: errorTest }, fn, function(err, result){ var now = new Date().getTime(); var duration = now - start; assert(duration >= (interval * (specialCount - 1)), 'did not include interval'); @@ -248,7 +248,7 @@ describe("retry", function () { return err && err === error; } var options = { - continueOperation: errorTest + errorFilter: errorTest }; async.retry(options, fn, _.rest(function(args) { assert.equal(callCount, 1, "did not retry the correct number of times"); diff --git a/mocha_test/retryable.js b/mocha_test/retryable.js index 9053dd1..897f83e 100644 --- a/mocha_test/retryable.js +++ b/mocha_test/retryable.js @@ -25,7 +25,7 @@ describe('retryable', function () { var calls = 0; var special = 'special'; var opts = { - continueOperation: function(err) { + errorFilter: function(err) { return err == special; } }; |