Async await wrapper for easy error handling
You need to use Node 7.6 (or later) with esnext target in order to use native async/await functionality. Target ES2015 (Node v4) or ES2016 (node v6) in your TypeScript compiler options if you are using an older version.
yarn add await-to-ts
OR
npm i --save await-to-ts
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 (err1) {
throw err1
}
console.log(s) // s is a string
}
main().catch(console.err.bind(console))