Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/scopsy/await-to-js
Async await wrapper for easy error handling without try-catch
https://github.com/scopsy/await-to-js
async async-await es2017 try-catch
Last synced: 3 days ago
JSON representation
Async await wrapper for easy error handling without try-catch
- Host: GitHub
- URL: https://github.com/scopsy/await-to-js
- Owner: scopsy
- License: mit
- Created: 2016-12-19T20:42:45.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-02-07T06:14:12.000Z (10 months ago)
- Last Synced: 2024-12-04T09:35:28.741Z (9 days ago)
- Topics: async, async-await, es2017, try-catch
- Language: TypeScript
- Homepage: http://blog.grossman.io/how-to-write-async-await-without-try-catch-blocks-in-javascript/
- Size: 214 KB
- Stars: 3,283
- Watchers: 22
- Forks: 153
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-github-star - await-to-js - catch | scopsy | 3062 | (TypeScript)
- awesome-list - await-to-js - catch | scopsy | 2051 | (TypeScript)
- awesome-star-libs - scopsy / await-to-js - catch 就可以轻松处理错误 (目录)
README
# await-to-js
[![NPM version][npm-image]][npm-url]
[![Downloads][download-badge]][npm-url]> Async await wrapper for easy error handling
Supported by:
The open-source notification infrastructure
## Pre-requisites
You need to use Node 7.6 (or later) or an ES7 transpiler in order to use async/await functionality.
You can use babel or typescript for that.## Install
```sh
npm i await-to-js --save
```## Usage
```js
import to from 'await-to-js';
// If you use CommonJS (i.e NodeJS environment), it should be:
// const to = require('await-to-js').default;async function asyncTaskWithCb(cb) {
let err, user, savedTask, notification;[ err, user ] = await to(UserModel.findById(1));
if(!user) return cb('No user found');[ err, savedTask ] = await to(TaskModel({userId: user.id, name: 'Demo Task'}));
if(err) return cb('Error occurred while saving task');if(user.notificationsEnabled) {
[ err ] = await to(NotificationService.sendNotification(user.id, 'Task Created'));
if(err) return cb('Error while sending notification');
}if(savedTask.assignedUser.id !== user.id) {
[ err, notification ] = await to(NotificationService.sendNotification(savedTask.assignedUser.id, 'Task was created for you'));
if(err) return cb('Error while sending notification');
}cb(null, savedTask);
}async function asyncFunctionWithThrow() {
const [err, user] = await to(UserModel.findById(1));
if (!user) throw new Error('User not found');
}
```## TypeScript usage
```javascript
interface ServerResponse {
test: number;
}const p = Promise.resolve({test: 123});
const [err, data] = await to(p);
console.log(data.test);
```## License
MIT © [Dima Grossman](http://blog.grossman.io) && Tomer Barnea
[npm-url]: https://npmjs.org/package/await-to-js
[npm-image]: https://img.shields.io/npm/v/await-to-js.svg?style=flat-square[travis-url]: https://travis-ci.org/scopsy/await-to-js
[travis-image]: https://img.shields.io/travis/scopsy/await-to-js.svg?style=flat-square[coveralls-url]: https://coveralls.io/r/scopsy/await-to-js
[coveralls-image]: https://img.shields.io/coveralls/scopsy/await-to-js.svg?style=flat-square[depstat-url]: https://david-dm.org/scopsy/await-to-js
[depstat-image]: https://david-dm.org/scopsy/await-to-js.svg?style=flat-square[download-badge]: http://img.shields.io/npm/dm/await-to-js.svg?style=flat-square