Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/boennemann/error-first-handler
https://github.com/boennemann/error-first-handler
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/boennemann/error-first-handler
- Owner: boennemann
- Created: 2015-02-02T17:36:27.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-10-26T02:38:41.000Z (about 8 years ago)
- Last Synced: 2024-10-13T18:29:15.511Z (2 months ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# error-first-handler
[![Build Status](https://travis-ci.org/boennemann/error-first-handler.svg)](https://travis-ci.org/boennemann/error-first-handler)
[![Dependency Status](https://david-dm.org/boennemann/error-first-handler.svg)](https://david-dm.org/boennemann/error-first-handler)
[![devDependency Status](https://david-dm.org/boennemann/error-first-handler/dev-status.svg)](https://david-dm.org/boennemann/error-first-handler#info=devDependencies)[![NPM](https://nodei.co/npm/error-first-handler.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/error-first-handler/)
> handles error first callbacks
```bash
npm i -S error-first-handler
```
```js
var fs = require('fs')
var efh = require('error-first-handler')var defaultHandler = efh()
fs.readFile('./package.json', defaultHandler(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 (data) {
// 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