error-first-handler
v1.0.1
Published
handles error first callbacks
Downloads
15
Maintainers
Readme
error-first-handler
handles error first callbacks
npm i -S error-first-handler
var fs = require('fs')
var efh = require('error-first-handler')
var default = efh()
fs.readFile('./package.json', default(function (data) {
// If an error occured it is thrown
// Otherwhise the error is shaved off the arguments array
}))
var custom = efh(function (err) {
console.log(err)
})
fs.readFile('./package.json', custom(function (data) {
// If an error occured it is logged
// Otherwhise the error is shaved off the arguments array
}))
var custom2 = efh(function (err) {
console.log(err)
return true
})
fs.readFile('./package.json', custom2(function (data) {
// If an error occured it is logged
// As the handler returns a truthy value the callback is executed
}))
// Passing errors to a parent callback is easy
function readFile(cb) {
fs.readFile('./package.json', efh(cb)(function {
// If an error occured it is handled by the parent callback
// Otherwhise the error is shaved off the arguments array
}))
}
MIT License 2015 Stephan Bönnemann