golangify
v0.3.1
Published
Golang like error handling for async/await
Downloads
2,264
Readme
golangify
Golang like error handling for async/await
Install
$ yarn add golangify
Usage
const go = require('golangify')
const main = async () => {
// Async
{
const success = x => Promise.resolve(x + x)
const failure = () => Promise.reject(new Error('err'))
console.log(await go(success)(1))
// => [ 2, null ]
const [result, err] = await go(failure)()
if (err !== null) {
console.log(err.message)
// => err
}
console.log(result)
// => null
}
// Sync
{
const successSync = x => x + x
const failureSync = () => new Error('err')
console.log(go(successSync)(1))
// => [ 2, null ]
const [result, err] = go(failureSync)()
if (err !== null) {
console.log(err.message)
// => err
}
console.log(result)
// => null
}
}
main()
API
golangify(func)
func
Type: function
Success
return [result, null]
Failure
return [null, err]
Contributors
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
License
MIT © akameco