
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
timeoutable
Advanced tools
Wrap timeouts around anything, to fail fast and/or keep user-facing code responsive.
A lightweight module written around the node callback-as-last-argument pattern, that interrupts if something is slow to respond.
You don't trust libraries you use, or the web services behind them to respond in a timely manner; and waiting potentially forever is worse than failing fast.
Your application uses several libraries communicating with different remote services, so their individual timeout options aren't adequate to fail fast.
You want to guard against callbacks being called more or less than once (once being the best number of times for a callback to be called)
var timeoutable = require('timeoutable');
timeoutable(2000, callback).run(function(callback){
thing.thatMightTakeMoreThanTwoSeconds(callback);
});
If the thing that might take more than two seconds actually takes more than two seconds, callback is invoked and passed an instance of TimeoutableError as the first argument. TimeoutableError is exported, so you can decide how severe of an error a timeout is:
var timeoutable = require('timeoutable');
var TimeoutableError = timeoutable.TimeoutableError;
function callback(err, response) {
if(err) {
if(err instanceof TimeoutableError) {
// idgaf, continue
} else {
return callback(err);
}
}
};
Still not a lot to it.
var timeoutable = require('timeoutable');
timeoutable expects a timeout duration, in milliseconds, and a callback; and returns an object with a "run" method.
timeoutable(5000, function() { alert('this does nothing') });
timeoutable(5000, function() { alert('hello') }).run(function(){
callback(); // this alerts "hello"
});
If you're just wrapping a function without any state, the above example holds.
Arguments are passed in as arrays:
timeoutable(100, function() {}).run(function(a, b, c) {
console.log(a, b, c);
}, [ 1, 2, 3 ])
// logs 1, 2, 3
If the timeoutable thing is a property of an instance of a "class", you need to do this:
something = {
state: 5,
editState: function(newState, callback) {
this.state = newState;
callback()
}
}
timeoutable(5000, function() { /* the callback */ })
.run(something, "editState", [ 4 ])
// something.state is now 4
This module absorbs things that might normally cause errors (example: callback being called multiple times, & the callback calls res.send); it tracks them, but doesn't complain unless you configure it to. (I need to add a bit more detail to this.)
Wrote this on a Sunday evening. Please open an issue, or a pull request, if you have ideas for improvements!
FAQs
Wrap timeouts around anything, to fail fast and/or keep user-facing code responsive.
The npm package timeoutable receives a total of 6 weekly downloads. As such, timeoutable popularity was classified as not popular.
We found that timeoutable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authorities’ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.