-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1027 from ajfranzoia/support-async-timeout
Support function timeout via async.timeout wrapper
- Loading branch information
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
export default function timeout(asyncFn, miliseconds) { | ||
var originalCallback, timer; | ||
var timedOut = false; | ||
|
||
function injectedCallback() { | ||
if (!timedOut) { | ||
originalCallback.apply(null, arguments); | ||
clearTimeout(timer); | ||
} | ||
} | ||
|
||
function timeoutCallback() { | ||
var error = new Error('Callback function timed out.'); | ||
error.code = 'ETIMEDOUT'; | ||
timedOut = true; | ||
originalCallback(error); | ||
} | ||
|
||
function injectCallback(asyncFnArgs) { | ||
// replace callback in asyncFn args | ||
var args = Array.prototype.slice.call(asyncFnArgs, 0); | ||
originalCallback = args[args.length - 1]; | ||
args[args.length - 1] = injectedCallback; | ||
return args; | ||
} | ||
|
||
function wrappedFn() { | ||
// setup timer and call original function | ||
timer = setTimeout(timeoutCallback, miliseconds); | ||
asyncFn.apply(null, injectCallback(arguments)); | ||
} | ||
|
||
return wrappedFn; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
var async = require('../lib'); | ||
var expect = require('chai').expect; | ||
|
||
describe('timeout', function () { | ||
|
||
it('timeout with series', function(done){ | ||
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) | ||
], | ||
function(err, results) { | ||
expect(err.message).to.equal('Callback function timed out.'); | ||
expect(err.code).to.equal('ETIMEDOUT'); | ||
expect(results[0]).to.equal('I didn\'t time out'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('timeout with parallel', function(done){ | ||
async.parallel([ | ||
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) | ||
], | ||
function(err, results) { | ||
expect(err.message).to.equal('Callback function timed out.'); | ||
expect(err.code).to.equal('ETIMEDOUT'); | ||
expect(results[0]).to.equal('I didn\'t time out'); | ||
done(); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters