golangify

Golang like error handling for async/await

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
golangify
0.3.14 years ago5 years agoMinified + gzip package size for golangify in KB

Readme

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