0.3.1 • Published 5 years ago

golangify v0.3.1

Weekly downloads
476
License
MIT
Repository
github
Last release
5 years ago

golangify

Build Status tested with jest styled with prettier All Contributors

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):

akameco💻 📖 ⚠️ 🚇

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT © akameco