error-first-handler

1.0.1 • Public • Published

error-first-handler

Build Status Dependency Status devDependency Status

NPM

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

Package Sidebar

Install

npm i error-first-handler

Weekly Downloads

4

Version

1.0.1

License

MIT

Last publish

Collaborators

  • boennemann