Func-It
Returns an empty function if one isn't present. Useful for optional callback parameters inside methods.
Usage
To ensure your callback parameter is actually a function and not another value or undefined follow this example. This allows you to make callbacks optional in your methods.
/*
* Calculates a result and saves it in the database. The callback is optional.
* callback(err, doc);
*/
function thisIsMyMethod (x, y, callback) {
// Ensure callback is an empty fn if the parameter isn't provided.
callback = require('func-it')(callback);
// Do something...
var result = x * y;
return database.save(result, callback); //no need to check typeof callback === 'function'.
}
Shorthand Empty Function
If you just need quick access to an empty function:
var funcIt = require('func-it');
var emptyFn = funcIt.emptyFn;
function doSomething() {
return emptyFn(result);
}