Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow functions wrapped in timeout to be called multiple times (fixes #1418) #1419

Merged
merged 1 commit into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions lib/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,32 @@ import wrapAsync from './internal/wrapAsync';
* });
*/
export default function timeout(asyncFn, milliseconds, info) {
var originalCallback, timer;
var timedOut = false;
var fn = wrapAsync(asyncFn);

function injectedCallback() {
if (!timedOut) {
originalCallback.apply(null, arguments);
clearTimeout(timer);
}
}
return initialParams(function (args, callback) {
var timedOut = false;
var timer;

function timeoutCallback() {
var name = asyncFn.name || 'anonymous';
var error = new Error('Callback function "' + name + '" timed out.');
error.code = 'ETIMEDOUT';
if (info) {
error.info = info;
function timeoutCallback() {
var name = asyncFn.name || 'anonymous';
var error = new Error('Callback function "' + name + '" timed out.');
error.code = 'ETIMEDOUT';
if (info) {
error.info = info;
}
timedOut = true;
callback(error);
}
timedOut = true;
originalCallback(error);
}

var fn = wrapAsync(asyncFn);
args.push(function () {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this style in ensureAsync. I think I prefer it over the args.concat(namedFunction) style. Should I change it back to the way it was before?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine. args.concat(namedFunction) is nice because it's a singe expression that returns the resulti, but it also creates a new array. push is fine here because nothing else re-uses the args array.

if (!timedOut) {
callback.apply(null, arguments);
clearTimeout(timer);
}
});

return initialParams(function (args, origCallback) {
originalCallback = origCallback;
// setup timer and call original function
timer = setTimeout(timeoutCallback, milliseconds);
fn.apply(null, args.concat(injectedCallback));
fn.apply(null, args);
});
}
36 changes: 36 additions & 0 deletions mocha_test/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,40 @@ describe('timeout', function () {
done();
});
});

it('timeout with multiple calls (#1418)', function(done) {
var timeout = async.timeout(function asyncFn(n, callback) {
if (n < 1) {
setTimeout(function() {
callback(null, 'I will time out');
}, 75);
} else {
async.setImmediate(function() {
callback(null, 'I didn\'t time out');
})
}
}, 50);

async.series([
function(cb) {
timeout(0, function(err, result) {
expect(err.message).to.equal('Callback function "asyncFn" timed out.');
expect(err.code).to.equal('ETIMEDOUT');
expect(err.info).to.equal(undefined);
expect(result).to.equal(undefined);
cb();
});
},
function(cb) {
timeout(1, function(err, result) {
expect(err).to.equal(null);
expect(result).to.equal('I didn\'t time out');
cb();
});
}
], function(err) {
expect(err).to.equal(null);
done();
});
})
});