Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/greylocklabs/await-result
Error handling for async functions without try/catch blocks
https://github.com/greylocklabs/await-result
async-await babel error-handling errors javascript nodejs promises
Last synced: about 6 hours ago
JSON representation
Error handling for async functions without try/catch blocks
- Host: GitHub
- URL: https://github.com/greylocklabs/await-result
- Owner: greylocklabs
- License: mit
- Created: 2017-06-15T23:45:04.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T23:18:12.000Z (almost 2 years ago)
- Last Synced: 2024-10-14T03:07:59.103Z (23 days ago)
- Topics: async-await, babel, error-handling, errors, javascript, nodejs, promises
- Language: TypeScript
- Homepage: https://npmjs.com/package/await-result
- Size: 2.39 MB
- Stars: 12
- Watchers: 2
- Forks: 0
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# await-result
> Error handling for async functions without try/catch blocks
---
[![npm version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![code coverage][coveralls-image]][coveralls-url]## Motivation
I find the way that errors are handled in Go to be elegant, and wanted to make something to emulate that behavior in
JavaScript while also taking advantage of the new async / await feature available in newer versions of Node.js (7.6+).## Installation
For use with Node.js 7.6 or later, unless you're using a transpiler such as [Babel](https://babeljs.io).
```bash
$ yarn add await-result
```Or, with `npm`:
```bash
$ npm install --save await-result
```## Usage
There are a few ways you can use this helper function:
### Get error and return value
You'll usually just call the function with only one argument - the function that returns a Promise:
```js
const [ err, data ] = await result(func());
```You can then do something with the error and return value without having to worry about try-catch
blocks.### Process error first
Similarly, you can get the error and return value, but process the error first; this is very useful in
larger projects where you might want to operate on the error in a consistent manner:```js
const [ processedErr, data ] = await result(func(), customErrorHandler);
```## Example
```js
import result from 'await-result';async function getNumUsers() {
const [ err, num ] = await result(database.userCount(), customErrorHandler);if (err) {
throw err;
}return num;
}getNumUsers().then((count) => {
console.log(`Total number of users: ${count}`);
}).catch((err) => {
console.log(err);
});
```## Contributing
See [CONTRIBUTING.md](CONTRIBUTING.md).
## License
MIT License. See [LICENSE](LICENSE) for details.
[npm-image]: https://img.shields.io/npm/v/await-result.svg?style=flat-square
[npm-url]: https://npmjs.org/package/await-result[travis-image]: https://img.shields.io/travis/greylocklabs/await-result.svg?style=flat-square
[travis-url]: https://travis-ci.org/greylocklabs/await-result[coveralls-image]: https://coveralls.io/repos/github/greylocklabs/await-result/badge.svg?branch=master
[coveralls-url]: https://coveralls.io/github/greylocklabs/await-result?branch=master