Golang like error handling for async/await
$ yarn add golangify
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()
Type: function
return [result, null]
return [null, err]
Thanks goes to these wonderful people (emoji key):
akameco 💻 📖 |
---|
This project follows the all-contributors specification. Contributions of any kind welcome!
MIT © akameco