Skip to content

gunar/go-for-it

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-for-it

CircleCI

Error handling à la golang.

Read the blog post:

Async Control Flow without Exceptions nor Monads

Installation

npm install go-for-it

Example

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.log

const 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