Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gunar/go-for-it
Error-handling à la golang.
https://github.com/gunar/go-for-it
Last synced: 6 days ago
JSON representation
Error-handling à la golang.
- Host: GitHub
- URL: https://github.com/gunar/go-for-it
- Owner: gunar
- Created: 2017-10-12T21:05:02.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-10-15T17:26:30.000Z (about 7 years ago)
- Last Synced: 2024-10-04T21:37:11.260Z (about 1 month ago)
- Language: JavaScript
- Homepage: https://medium.com/@gunar/async-control-flow-without-exceptions-nor-monads-b19af2acc553
- Size: 31.3 KB
- Stars: 16
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# go-for-it
[![CircleCI](https://circleci.com/gh/gunar/go-for-it.svg?style=svg)](https://circleci.com/gh/gunar/go-for-it)
Error handling à la golang.
Read the blog post:
> [Async Control Flow without Exceptions nor Monads](https://medium.com/@gunar/async-control-flow-without-exceptions-nor-monads-b19af2acc553)## Installation
```
npm install go-for-it
```## Example
```js
const go = require('go-for-it')const toUpper = async string => {
if (string === 'invalid') throw Error('Invalid input')
return string.toUpperCase()
}
const errorHandler = () => { console.log('There has been an error. I\'ll handle it.') }
const print = console.logconst foo = async input => {
const [err, value] = await go(toUpper(input))
if (err) return errorHandler(err)
print(value)
}// Works normally.
foo('gunar')
// "GUNAR"// Business Logic Error gets handled by errorHandler().
foo('invalid')
// "There has been an error. I'll handle it."// Runtime Exceptions DO NOT get handled by errorHandler(),
foo(555555).catch(e => {
// but can be caught.
console.log(e)
// TypeError: string.toUpperCase is not a function
})
```## License
MIT [http://gunar.mit-license.org]()