callback-timeout
Advanced tools
Comparing version 2.0.0 to 2.1.0
var defineError = require('define-error') | ||
module.exports.TimeoutError = defineError('TimeoutError') | ||
module.exports.TimeoutError = defineError('TimeoutError', TimeoutError) | ||
function TimeoutError () { | ||
this.code = 'ETIMEOUT' | ||
} |
@@ -7,13 +7,13 @@ var timeout = require('..') | ||
doSomethingFast(timeout(function doSomethingFastHandler (err) { | ||
if (err) | ||
console.log(err.message) // Will not happen | ||
else | ||
console.log('doSomethingFastHandler executed without error.') // Will happen | ||
if (err) | ||
console.log(err.code, err.message) // Will not happen | ||
else | ||
console.log('doSomethingFastHandler executed without error') // Will happen | ||
}, 1000)) | ||
doSomethingSlow(timeout(function doSomethingSlowHandler (err) { | ||
if (err) | ||
console.log(err.message) // Will happen | ||
else | ||
console.log('doSomethingSlowHandler executed without error.') // Will not happen | ||
if (err) | ||
console.log(err.code, err.message) // ETIMEOUT ... | ||
else | ||
console.log('doSomethingSlowHandler executed without error') // Won't happen | ||
}, 1000)) |
31
index.js
@@ -1,20 +0,23 @@ | ||
var TimeoutError = require('./errors').TimeoutError | ||
var errors = require('./errors') | ||
module.exports = function callbackTimeout (f, t, e) { | ||
if (!t) return f | ||
var timer = setTimeout(onTimeout, t) | ||
return callback | ||
if (!t) return f | ||
var timer = setTimeout(onTimeout, t) | ||
return callback | ||
function onTimeout () { | ||
clearTimeout(timer) | ||
timer = null | ||
f.call(f, new TimeoutError(e || 'timeout of ' + t + 'ms exceeded for callback ' + (f.name || 'anonymous'))) | ||
} | ||
function onTimeout () { | ||
clearTimeout(timer) | ||
timer = null | ||
var msg = e || 'timeout of ' + | ||
t + 'ms exceeded for callback ' + | ||
(f.name || 'anonymous') | ||
f.call(f, new errors.TimeoutError(msg)) | ||
} | ||
function callback () { | ||
if (timer) { | ||
clearTimeout(timer) | ||
f.apply(f, arguments) | ||
} | ||
function callback () { | ||
if (timer) { | ||
clearTimeout(timer) | ||
f.apply(f, arguments) | ||
} | ||
} | ||
} |
{ | ||
"name": "callback-timeout", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Invokes callback with single error argument if timeout occurs before it's invoked by other means", | ||
@@ -21,10 +21,11 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@jasonpincin/standard": "~5.0.0-7", | ||
"istanbul": "~0.3.17", | ||
"@jasonpincin/standard": "~5.3.1-3", | ||
"eventuate-once": "~1.1.0", | ||
"faucet": "0.0.1", | ||
"istanbul": "~0.4.0", | ||
"opn": "~1.0.2", | ||
"phantomjs": "~1.9.18", | ||
"tap-dot": "~1.0.0", | ||
"tap-spec": "~4.0.2", | ||
"tape": "~4.0.3", | ||
"zuul": "~3.2.0" | ||
"phantomjs-prebuilt": "~2.1.7", | ||
"snazzy": "~4.0.0", | ||
"tape": "~4.5.1", | ||
"zuul": "~3.10.1" | ||
}, | ||
@@ -37,9 +38,19 @@ "author": { | ||
"dependencies": { | ||
"define-error": "~1.0.0" | ||
"define-error": "~1.1.0" | ||
}, | ||
"scripts": { | ||
"test": "make npm-test", | ||
"coverage": "make npm-coverage", | ||
"browser-tests": "zuul -- test/*.js" | ||
"test": "npm run -s faucet && npm run -s lint && npm run -s check-cover", | ||
"test-nocover": "npm run -s faucet-nocover && npm run -s lint", | ||
"travis-test": "npm run -s tap && ((cat coverage/lcov.info | coveralls) || exit 0)", | ||
"travis-browser-test": "zuul -- test/*.js", | ||
"faucet": "istanbul cover --report lcov --print none -- tape test/*.js | faucet", | ||
"faucet-nocover": "tape test/**/*.js | faucet", | ||
"tap": "istanbul cover --report lcov --print none -- tape test/*.js", | ||
"tap-nocover": "tape test/**/*.js", | ||
"phantom": "zuul --phantom -- test/*.js | faucet", | ||
"lint": "standard | snazzy", | ||
"check-cover": "istanbul check-coverage --statements 100 --branches 100 --functions 100 --lines 100 2>&1 | grep ERROR: ; (test ${PIPESTATUS[0]} -eq 0) || (istanbul report text; exit 1)", | ||
"view-cover": "istanbul report text", | ||
"open-cover": "open coverage/lcov-report/index.html" | ||
} | ||
} |
@@ -7,2 +7,3 @@ callback-timeout | ||
[![Coverage Status](https://coveralls.io/repos/jasonpincin/callback-timeout/badge.png?branch=master)](https://coveralls.io/r/jasonpincin/callback-timeout?branch=master) | ||
[![Sauce Test Status](https://saucelabs.com/browser-matrix/jp-callback-timeout.svg)](https://saucelabs.com/u/jp-callback-timeout) | ||
@@ -20,13 +21,13 @@ Executes callback with single error argument if timeout is exceeded before it's called naturally | ||
doSomethingFast(timeout(function doSomethingFastHandler (err) { | ||
if (err) | ||
console.log(err.message) // Will not happen | ||
else | ||
console.log('doSomethingFastHandler executed without error.') // Will happen | ||
if (err) | ||
console.log(err.code, err.message) // Will not happen | ||
else | ||
console.log('doSomethingFastHandler executed without error.') // Will happen | ||
}, 1000)) | ||
doSomethingSlow(timeout(function doSomethingSlowHandler (err) { | ||
if (err) | ||
console.log(err.message) // Will happen | ||
else | ||
console.log('doSomethingSlowHandler executed without error.') // Will not happen | ||
if (err) | ||
console.log(err.code, err.message) // ETIMEOUT ... | ||
else | ||
console.log('doSomethingSlowHandler executed without error.') // Will not happen | ||
}, 1000)) | ||
@@ -49,3 +50,4 @@ ``` | ||
The constructor of the error thrown when a timeout occurs. | ||
The constructor of the error supplied to the `callback` when a timeout occurs. | ||
TimeoutError objects will have a `code` property with the value `ETIMEOUT`. | ||
@@ -62,10 +64,11 @@ ## install | ||
`npm test [--dot | --spec] [--grep=pattern]` | ||
`npm test [--dot | --spec] [--phantom] [--grep=pattern]` | ||
Specifying `--dot` or `--spec` will change the output from the default TAP style. | ||
Specifying `--phantom` will cause the tests to run in the headless phantom browser instead of node. | ||
Specifying `--grep` will only run the test files that match the given pattern. | ||
### browser tests | ||
### browser test | ||
`npm run browser-tests` | ||
`npm run browser-test` | ||
@@ -72,0 +75,0 @@ This will run the tests in all browsers (specified in .zuul.yml). Be sure to [educate zuul](https://github.com/defunctzombie/zuul/wiki/cloud-testing#2-educate-zuul) first. |
@@ -5,24 +5,25 @@ var test = require('tape'), | ||
test('with error message provided', function (t) { | ||
t.plan(3) | ||
t.plan(3) | ||
function doSomethingFast (cb) { setTimeout(cb, 50) } | ||
function doSomethingSlow (cb) { setTimeout(cb, 500) } | ||
function doSomethingFast (cb) { setTimeout(cb, 50) } | ||
function doSomethingSlow (cb) { setTimeout(cb, 500) } | ||
doSomethingFast(timeout(function doSomethingFastHandler (err) { | ||
if (err) | ||
t.fail('doSomethingFastHandler got an error') | ||
else | ||
t.pass('doSomethingFastHandler did not get an error') | ||
}, 1000, 'fast function timed out')) | ||
doSomethingFast(timeout(function doSomethingFastHandler (err) { | ||
if (err) | ||
t.fail('doSomethingFastHandler got an error') | ||
else | ||
t.pass('doSomethingFastHandler did not get an error') | ||
}, 1000, 'fast function timed out')) | ||
doSomethingSlow(timeout(function doSomethingSlowHandler (err) { | ||
if (err) { | ||
t.pass('doSomethingSlowHandler got error') | ||
t.ok(err.message.indexOf('slow function timed out') > -1, 'callback err had custom error message') | ||
} | ||
else { | ||
t.fail('doSomethingSlowHandler did not get an error') | ||
} | ||
}, 250, 'slow function timed out')) | ||
doSomethingSlow(timeout(function doSomethingSlowHandler (err) { | ||
if (err) { | ||
t.pass('doSomethingSlowHandler got error') | ||
t.ok(err.message.indexOf('slow function timed out') > -1, | ||
'callback err had custom error message') | ||
} | ||
else { | ||
t.fail('doSomethingSlowHandler did not get an error') | ||
} | ||
}, 250, 'slow function timed out')) | ||
}) | ||
@@ -5,27 +5,27 @@ var test = require('tape'), | ||
test('with no timeout given', function (t) { | ||
t.plan(3) | ||
t.plan(3) | ||
function doSomethingSlow (cb) { setTimeout(cb, 1000) } | ||
function doSomethingSlow (cb) { setTimeout(cb, 1000) } | ||
doSomethingSlow(timeout(function doSomethingSlowZeroHandler (err) { | ||
if (err) | ||
t.fail('got an error on 0 timeout') | ||
else | ||
t.pass('got no error on 0 timeout') | ||
}, 0)) | ||
doSomethingSlow(timeout(function doSomethingSlowZeroHandler (err) { | ||
if (err) | ||
t.fail('got an error on 0 timeout') | ||
else | ||
t.pass('got no error on 0 timeout') | ||
}, 0)) | ||
doSomethingSlow(timeout(function doSomethingSlowNullHandler (err) { | ||
if (err) | ||
t.fail('got an error on null timeout') | ||
else | ||
t.pass('got no error on null timeout') | ||
}, null)) | ||
doSomethingSlow(timeout(function doSomethingSlowNullHandler (err) { | ||
if (err) | ||
t.fail('got an error on null timeout') | ||
else | ||
t.pass('got no error on null timeout') | ||
}, null)) | ||
doSomethingSlow(timeout(function doSomethingSlowUndefinedHandler (err) { | ||
if (err) | ||
t.fail('got an error on undefined timeout') | ||
else | ||
t.pass('got no error on undefined timeout') | ||
}, undefined)) | ||
doSomethingSlow(timeout(function doSomethingSlowUndefinedHandler (err) { | ||
if (err) | ||
t.fail('got an error on undefined timeout') | ||
else | ||
t.pass('got no error on undefined timeout') | ||
}, undefined)) | ||
}) | ||
@@ -6,27 +6,28 @@ var test = require('tape'), | ||
test('with timeouts given', function (t) { | ||
t.plan(5) | ||
t.plan(5) | ||
function doSomethingFast (cb) { setTimeout(cb, 100) } | ||
function doSomethingSlow (cb) { setTimeout(cb, 2000) } | ||
function doSomethingFast (cb) { setTimeout(cb, 100) } | ||
function doSomethingSlow (cb) { setTimeout(cb, 2000) } | ||
doSomethingFast(timeout(function doSomethingFastHandler (err) { | ||
if (err) | ||
t.fail('doSomethingFastHandler got an error') | ||
else | ||
t.pass('doSomethingFastHandler did not get an error') | ||
}, 1000)) | ||
doSomethingFast(timeout(function doSomethingFastHandler (err) { | ||
if (err) | ||
t.fail('doSomethingFastHandler got an error') | ||
else | ||
t.pass('doSomethingFastHandler did not get an error') | ||
}, 1000)) | ||
doSomethingSlow(timeout(function doSomethingSlowHandler (err) { | ||
if (err) | ||
t.pass('doSomethingSlowHandler got error') | ||
else | ||
t.fail('doSomethingSlowHandler did not get an error') | ||
}, 1000)) | ||
doSomethingSlow(timeout(function doSomethingSlowHandler (err) { | ||
if (err) | ||
t.pass('doSomethingSlowHandler got error') | ||
else | ||
t.fail('doSomethingSlowHandler did not get an error') | ||
}, 1000)) | ||
doSomethingSlow(timeout(function (err) { | ||
t.ok(err.message.indexOf('anonymous') > -1, 'callback err has proper message for anonymous functions') | ||
t.ok(err instanceof TimeoutError, 'error is a TimeoutError') | ||
t.equals(err.name, 'TimeoutError', 'error.name is TimeoutError') | ||
}, 250)) | ||
doSomethingSlow(timeout(function (err) { | ||
t.ok(err.message.indexOf('anonymous') > -1, | ||
'callback err has proper message for anonymous functions') | ||
t.ok(err instanceof TimeoutError, 'error is a TimeoutError') | ||
t.equals(err.name, 'TimeoutError', 'error.name is TimeoutError') | ||
}, 250)) | ||
}) | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
123
79
11127
9
+ Addeddefine-error@1.1.0(transitive)
- Removedcapture-stack-trace@1.0.2(transitive)
- Removeddefine-error@1.0.0(transitive)
Updateddefine-error@~1.1.0