Skip to content

Commit b26955f

Browse files
authored
Merge pull request #1231 from caolan/retry
Retry should pass all of the resolve arguments to the callback
2 parents a46e3b7 + 5aa942d commit b26955f

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ Another big performance win has been re-implementing `queue`, `cargo`, and `prio
4040
- Added `autoInject`, a relative of `auto` that automatically spreads a task's dependencies as arguments to the task function. (#608, #1055, #1099, #1100)
4141
- You can now limit the concurrency of `auto` tasks. (#635, #637)
4242
- Added `retryable`, a relative of `retry` that wraps an async function, making it retry when called. (#1058)
43-
- `retry` now supports specifying a function that determines the next time interval, useful for exponential backoff and other retry strategies. (#1161)
43+
- `retry` now supports specifying a function that determines the next time interval, useful for exponential backoff, logging and other retry strategies. (#1161)
44+
- `retry` will now pass all of the arguments the task function was resolved with to the callback (#1231).
4445
- Added `q.unsaturated` -- callback called when a `queue`'s number of running workers falls below a threshold. (#868, #1030, #1033, #1034)
4546
- Added `q.error` -- a callback called whenever a `queue` task calls its callback with an error. (#1170)
4647
- `applyEach` and `applyEachSeries` now pass results to the final callback. (#1088)

lib/retry.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ export default function retry(opts, task, callback) {
109109

110110
var attempt = 1;
111111
function retryAttempt() {
112-
task(function(err, result) {
112+
task(function(err) {
113113
if (err && attempt++ < options.times) {
114114
setTimeout(retryAttempt, options.intervalFunc(attempt));
115115
} else {
116-
callback(err, result);
116+
callback.apply(null, arguments);
117117
}
118118
});
119119
}

mocha_test/retry.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var async = require('../lib');
22
var expect = require('chai').expect;
33
var assert = require('assert');
4+
var _ = require('lodash');
45

56
describe("retry", function () {
67

@@ -121,7 +122,7 @@ describe("retry", function () {
121122
}, 50);
122123
});
123124

124-
it('retry does not precompute the intervals (#1226)',function(done) {
125+
it('retry does not precompute the intervals (#1226)', function(done) {
125126
var callTimes = [];
126127
function intervalFunc() {
127128
callTimes.push(new Date().getTime());
@@ -136,4 +137,24 @@ describe("retry", function () {
136137
done();
137138
});
138139
});
140+
141+
it('retry passes all resolve arguments to callback', function(done) {
142+
function fn(callback) {
143+
callback(null, 1, 2, 3); // respond with indexed values
144+
}
145+
async.retry(5, fn, _.rest(function(args) {
146+
expect(args).to.be.eql([null, 1, 2, 3]);
147+
done();
148+
}));
149+
});
150+
151+
// note this is a synchronous test ensuring retry is synchrnous in the fastest (most straightforward) case
152+
it('retry calls fn immediately and will call callback if successful', function() {
153+
function fn(callback) {
154+
callback(null, {a: 1});
155+
}
156+
async.retry(5, fn, function(err, result) {
157+
expect(result).to.be.eql({a: 1});
158+
});
159+
})
139160
});

0 commit comments

Comments
 (0)