Ensures a callback function gets called, one way or another. Use in situations where continuing is more important than the result of the callback. I generally incorporate this into my shutdown / restart procedure.
npm install --save-dev failsafe
// Failsafe code - will definitely exit in 15 seconds or less.
var failsafe = require('failsafe');
database.close(failsafe(5000, function(){
server.close(failsafe(10000, function(){
console.log('Server shutdown');
process.exit();
})
})
// Original code - might not ever exit
database.close(function(){
server.close(function(){
console.log('Server shutdown');
process.exit();
})
})
It's so simple, I'll put the source code right here:
// failsafe.js
var once = require('once');
module.exports = function(delay, callback) {
callback = once(callback);
setTimeout(callback, delay);
return callback;
};
Pull requests welcome!