failsafe

1.0.0 • Public • Published

failsafe

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.

Install

npm install --save-dev failsafe

Usage

// 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();
  })
})

How it works

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;
};

Contributing

Pull requests welcome!

Readme

Keywords

Package Sidebar

Install

npm i failsafe

Weekly Downloads

23

Version

1.0.0

License

ISC

Last publish

Collaborators

  • wmhilton