node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »


Safe function callbacks

Assures asynchronous functions always call the callback instead of throwing errors under some circumstances.

Asynchronous functions that receive a callback shall always call the callback function, with or without error, but shall never throw and error as the user of the function may not expect that behavior, assuming the callback will called in case of any issue. Unfortunately, some popular libraries do not behave.

The general rule is that a function may deliver operational errors synchronously (e.g., by throwing) or asynchronously (by passing them to a callback or emitting error on an EventEmitter), but it should not do both.


npm install safe-callback


Given a fn that uses the node-style callbacks, expecting a callback as the last parameter and this callback expecting an err as the first parameter, you can obtain a new function that will always call the callback and never throw an error:

var shield = require('safe-callback')
function fn(argument, callback) {
  // sometimes throws, sometimes calls the callback... 
var safeFn = shield(fn)
safeFn(args, function (err, result) {
  // if `fn` throws, `err` will contain the thrown error 


shield(fn, [context])


  • fn is a function that receives a callback as the last. argument. The callback will receive an err object as the first argument.
  • context is the object uses as this when calling fn.

Returns value

A new function that wraps fn and makes it safe to call.