https://github.com/phra/await-to-ts
http://blog.grossman.io/how-to-write-async-await-without-try-catch-blocks-in-javascript/
https://github.com/phra/await-to-ts
Last synced: over 1 year ago
JSON representation
http://blog.grossman.io/how-to-write-async-await-without-try-catch-blocks-in-javascript/
- Host: GitHub
- URL: https://github.com/phra/await-to-ts
- Owner: phra
- License: other
- Created: 2017-03-29T10:15:35.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T23:58:35.000Z (over 3 years ago)
- Last Synced: 2025-03-13T23:47:49.693Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 1.48 MB
- Stars: 18
- Watchers: 2
- Forks: 2
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# await-to-ts
[](https://greenkeeper.io/)
[![NPM version][npm-image]][npm-url]
[![Downloads][download-badge]][npm-url]
[![Coveralls][coveralls-image]][coveralls-url]
> Async await wrapper for easy error handling
## Pre-requisites
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.
## Install
```sh
yarn add await-to-ts
```
OR
```sh
npm i --save await-to-ts
```
## Usage
```js
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))
```
[npm-url]: https://npmjs.org/package/await-to-ts
[npm-image]: https://img.shields.io/npm/v/await-to-ts.svg?style=flat-square
[travis-url]: https://travis-ci.org/phra/await-to-ts
[travis-image]: https://img.shields.io/travis/phra/await-to-ts.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/phra/await-to-ts
[coveralls-image]: https://img.shields.io/coveralls/phra/await-to-ts.svg?style=flat-square
[depstat-url]: https://david-dm.org/phra/await-to-ts
[depstat-image]: https://david-dm.org/phra/await-to-ts.svg?style=flat-square
[download-badge]: https://img.shields.io/npm/dt/await-to-ts.svg?style=flat-square