Skip to content

Commit

Permalink
Merge pull request #1098 from asilvas/named-timeout
Browse files Browse the repository at this point in the history
#1097. Named timeout for tracking purposes
  • Loading branch information
aearly committed Apr 7, 2016
2 parents 0a3c1b1 + 1d50e72 commit 6ad036d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2089,13 +2089,15 @@ __Arguments__

* `function` - The asynchronous function you want to set the time limit.
* `miliseconds` - The specified time limit.
* `info` - *Optional* Any variable you want attached (`string`, `object`, etc) to
timeout Error for more information.

__Example__

```js
async.timeout(function(callback) {
async.timeout(function nameOfCallback(callback) {
doAsyncTask(callback);
}, 1000);
}, 1000, 'more info about timeout');
```

---------------------------------------
Expand Down
8 changes: 6 additions & 2 deletions lib/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import initialParams from './internal/initialParams';

export default function timeout(asyncFn, miliseconds) {
export default function timeout(asyncFn, miliseconds, info) {
var originalCallback, timer;
var timedOut = false;

Expand All @@ -14,8 +14,12 @@ export default function timeout(asyncFn, miliseconds) {
}

function timeoutCallback() {
var error = new Error('Callback function timed out.');
var name = asyncFn.name || 'anonymous';
var error = new Error('Callback function "' + name + '" timed out.');
error.code = 'ETIMEDOUT';
if (info) {
error.info = info;
}
timedOut = true;
originalCallback(error);
}
Expand Down
4 changes: 2 additions & 2 deletions mocha_test/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('timeout', function () {
}, 50)
],
function(err, results) {
expect(err.message).to.equal('Callback function timed out.');
expect(err.message).to.equal('Callback function "asyncFn" timed out.');
expect(err.code).to.equal('ETIMEDOUT');
expect(results[0]).to.equal('I didn\'t time out');
done();
Expand All @@ -38,7 +38,7 @@ describe('timeout', function () {
}, 50)
],
function(err, results) {
expect(err.message).to.equal('Callback function timed out.');
expect(err.message).to.equal('Callback function "asyncFn" timed out.');
expect(err.code).to.equal('ETIMEDOUT');
expect(results[0]).to.equal('I didn\'t time out');
done();
Expand Down
35 changes: 31 additions & 4 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -2770,7 +2770,7 @@ exports['asyncify'] = {
};

exports['timeout'] = function (test) {
test.expect(3);
test.expect(4);

async.series([
async.timeout(function asyncFn(callback) {
Expand All @@ -2785,15 +2785,41 @@ exports['timeout'] = function (test) {
}, 150)
],
function(err, results) {
test.ok(err.message === 'Callback function timed out.');
test.ok(err.message === 'Callback function "asyncFn" timed out.');
test.ok(err.code === 'ETIMEDOUT');
test.ok(err.info === undefined);
test.ok(results[0] === 'I didn\'t time out');
test.done();
});
};

exports['timeout with info'] = function (test) {
test.expect(4);

var info = { custom: 'info about callback' };
async.series([
async.timeout(function asyncFn(callback) {
setTimeout(function() {
callback(null, 'I didn\'t time out');
}, 50);
}, 200),
async.timeout(function asyncFn(callback) {
setTimeout(function() {
callback(null, 'I will time out');
}, 300);
}, 150, info)
],
function(err, results) {
test.ok(err.message === 'Callback function "asyncFn" timed out.');
test.ok(err.code === 'ETIMEDOUT');
test.ok(err.info === info);
test.ok(results[0] === 'I didn\'t time out');
test.done();
});
};

exports['timeout with parallel'] = function (test) {
test.expect(3);
test.expect(4);

async.parallel([
async.timeout(function asyncFn(callback) {
Expand All @@ -2808,8 +2834,9 @@ exports['timeout with parallel'] = function (test) {
}, 150)
],
function(err, results) {
test.ok(err.message === 'Callback function timed out.');
test.ok(err.message === 'Callback function "asyncFn" timed out.');
test.ok(err.code === 'ETIMEDOUT');
test.ok(err.info === undefined);
test.ok(results[0] === 'I didn\'t time out');
test.done();
});
Expand Down

0 comments on commit 6ad036d

Please sign in to comment.