summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorasilvas <asilvas@godaddy.com>2016-04-05 15:28:40 -0700
committerasilvas <asilvas@godaddy.com>2016-04-05 15:28:40 -0700
commitcb703585a83d48a894929c41094a33605c687f97 (patch)
tree5c521ceb9d922bdbcfcd030a2b2202799be1bc1c
parent5d2f19b360b7ec74db659ae571d730c1acc58b76 (diff)
downloadasync-cb703585a83d48a894929c41094a33605c687f97.tar.gz
#1097. Named timeout for tracking purposes
-rw-r--r--README.md6
-rw-r--r--lib/timeout.js8
-rw-r--r--mocha_test/timeout.js4
-rwxr-xr-xtest/test-async.js35
4 files changed, 43 insertions, 10 deletions
diff --git a/README.md b/README.md
index 79ced0a..10b4cd7 100644
--- a/README.md
+++ b/README.md
@@ -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');
```
---------------------------------------
diff --git a/lib/timeout.js b/lib/timeout.js
index 6af4f52..ebf0446 100644
--- a/lib/timeout.js
+++ b/lib/timeout.js
@@ -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;
@@ -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);
}
diff --git a/mocha_test/timeout.js b/mocha_test/timeout.js
index c8b79a7..7608124 100644
--- a/mocha_test/timeout.js
+++ b/mocha_test/timeout.js
@@ -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();
@@ -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();
diff --git a/test/test-async.js b/test/test-async.js
index 6831ac2..d36c704 100755
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -2770,7 +2770,7 @@ exports['asyncify'] = {
};
exports['timeout'] = function (test) {
- test.expect(3);
+ test.expect(4);
async.series([
async.timeout(function asyncFn(callback) {
@@ -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) {
@@ -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();
});