Skip to content

Commit 0af976c

Browse files
committed
rename the new retry option to errorFilter and consolidate retry attempt condition into one statement
1 parent 0337ee0 commit 0af976c

3 files changed

Lines changed: 16 additions & 20 deletions

File tree

lib/retry.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import constant from 'lodash/constant';
1919
* * `interval` - The time to wait between retries, in milliseconds. The
2020
* default is `0`. The interval may also be specified as a function of the
2121
* retry count (see example).
22-
* * `continueOperation` - An optional synchronous function that is invoked on
23-
* erroneous result with the the error. If it returns `true` the retry attempts
24-
* will continue, if the function returns `false` the retry flow is aborted
25-
* with the current attempt's error and result being returned to the final
26-
* callback. Invoked with (err).
22+
* * `errorFilter` - An optional synchronous function that is invoked on
23+
* erroneous result. If it returns `true` the retry attempts will continue;
24+
* if the function returns `false` the retry flow is aborted with the current
25+
* attempt's error and result being returned to the final callback.
26+
* Invoked with (err, result).
2727
* * If `opts` is a number, the number specifies the number of times to retry,
2828
* with the default interval of `0`.
2929
* @param {Function} task - A function which receives two arguments: (1) a
@@ -70,7 +70,7 @@ import constant from 'lodash/constant';
7070
* // try calling apiMethod only when error condition satisfies, all other
7171
* // errors will abort the retry control flow and return to final callback
7272
* async.retry({
73-
* continueOperation: function(err) {
73+
* errorFilter: function(err) {
7474
* return err.message === 'Temporary error'; // only retry on a specific error
7575
* }
7676
* }, apiMethod, function(err, result) {
@@ -104,7 +104,7 @@ export default function retry(opts, task, callback) {
104104
t.interval :
105105
constant(+t.interval || DEFAULT_INTERVAL);
106106

107-
acc.continueOperation = t.continueOperation;
107+
acc.errorFilter = t.errorFilter;
108108
} else if (typeof t === 'number' || typeof t === 'string') {
109109
acc.times = +t || DEFAULT_TIMES;
110110
} else {
@@ -127,14 +127,10 @@ export default function retry(opts, task, callback) {
127127
var attempt = 1;
128128
function retryAttempt() {
129129
task(function(err) {
130-
if (err && attempt++ < options.times) {
131-
var proceed = typeof options.continueOperation != 'function' ||
132-
options.continueOperation(err);
133-
if(proceed) {
134-
setTimeout(retryAttempt, options.intervalFunc(attempt));
135-
} else {
136-
callback.apply(null, arguments);
137-
}
130+
if (err && attempt++ < options.times &&
131+
(typeof options.errorFilter != 'function' ||
132+
options.errorFilter(err))) {
133+
setTimeout(retryAttempt, options.intervalFunc(attempt));
138134
} else {
139135
callback.apply(null, arguments);
140136
}

mocha_test/retry.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ describe("retry", function () {
173173
}
174174
var options = {
175175
times: times,
176-
continueOperation: errorTest
176+
errorFilter: errorTest
177177
};
178178
async.retry(options, fn, function(err, result){
179179
assert.equal(callCount, 3, "did not retry the correct number of times");
@@ -197,7 +197,7 @@ describe("retry", function () {
197197
return err && err === error + callCount; // just a different pattern
198198
}
199199
var options = {
200-
continueOperation: errorTest
200+
errorFilter: errorTest
201201
};
202202
async.retry(options, fn, function(err, result){
203203
assert.equal(callCount, 2, "did not retry the correct number of times");
@@ -223,7 +223,7 @@ describe("retry", function () {
223223
return err && err !== special;
224224
}
225225
var start = new Date().getTime();
226-
async.retry({ interval: interval, continueOperation: errorTest }, fn, function(err, result){
226+
async.retry({ interval: interval, errorFilter: errorTest }, fn, function(err, result){
227227
var now = new Date().getTime();
228228
var duration = now - start;
229229
assert(duration >= (interval * (specialCount - 1)), 'did not include interval');
@@ -248,7 +248,7 @@ describe("retry", function () {
248248
return err && err === error;
249249
}
250250
var options = {
251-
continueOperation: errorTest
251+
errorFilter: errorTest
252252
};
253253
async.retry(options, fn, _.rest(function(args) {
254254
assert.equal(callCount, 1, "did not retry the correct number of times");

mocha_test/retryable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('retryable', function () {
2525
var calls = 0;
2626
var special = 'special';
2727
var opts = {
28-
continueOperation: function(err) {
28+
errorFilter: function(err) {
2929
return err == special;
3030
}
3131
};

0 commit comments

Comments
 (0)