await-to-ts
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

await-to-ts

NPM version Downloads Coveralls

Async await wrapper for easy error handling

Install

yarn add await-to-ts

OR

npm i --save await-to-ts

Usage

import to from 'await-to-ts'
 
const f = () => Promise.resolve(42)
const g = () => Promise.resolve('42')
 
async function main() {
  const [err, n] = await to(f())
  if (err) {
    throw err
  }
 
  console.log(n) // n is a number
 
  const [err1, s] = await to(g())
  if (err) {
    throw err
  }
 
  console.log(s) // s is a string
}
 
main()

Package Sidebar

Install

npm i await-to-ts

Weekly Downloads

130

Version

1.0.6

License

MIT

Last publish

Collaborators

  • phra