summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Early <alexander.early@gmail.com>2016-04-07 13:45:55 -0700
committerAlex Early <alexander.early@gmail.com>2016-04-07 13:45:55 -0700
commit6ad036d07add9aa24a26b831cd211041d3b681f6 (patch)
tree3b448ba60f3c5cee45a43bdd7672e93687cb17e6
parent0a3c1b14654ae8b58839cefdad1e8aa8cf147631 (diff)
parent1d50e729d8fe50bec8e6542145668425ebb829c4 (diff)
downloadasync-6ad036d07add9aa24a26b831cd211041d3b681f6.tar.gz
Merge pull request #1098 from asilvas/named-timeout
#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 f18239c..77c14ce 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..b77ba78 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();
});