Skip to content

Commit

Permalink
rename the new retry option to errorFilter and consolidate retry atte…
Browse files Browse the repository at this point in the history
…mpt condition into one statement
  • Loading branch information
bojand committed Aug 5, 2016
1 parent 0337ee0 commit 0af976c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
26 changes: 11 additions & 15 deletions lib/retry.js
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions mocha_test/retry.js
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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');
Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion mocha_test/retryable.js
Expand Up @@ -25,7 +25,7 @@ describe('retryable', function () {
var calls = 0;
var special = 'special';
var opts = {
continueOperation: function(err) {
errorFilter: function(err) {
return err == special;
}
};
Expand Down

0 comments on commit 0af976c

Please sign in to comment.