Skip to content

Ensure a callback function gets called, one way or another!

License

Notifications You must be signed in to change notification settings

billiegoose/failsafe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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!

About

Ensure a callback function gets called, one way or another!

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published