# catch-await

> Async/await wrapper for easy error handling in js

Latest version **1.0.1** (published 2019-10-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install catch-await
pnpm add catch-await
yarn add catch-await
bun add catch-await
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2019-10-11 |
| First published | 2019-10-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=6.0.0 |
| Dependencies | 0 |
| Unpacked size | 7.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Roman Chasovitin |
| Maintainers | rchasovitin |
| Keywords | node, async, await, async await, catch, await catch |

## Links

- npm: https://www.npmjs.com/package/catch-await
- Repository: https://github.com/RomanChasovitin/catch-await
- Issues: https://github.com/RomanChasovitin/catch-await/issues
- npm.io page: https://npm.io/package/catch-await

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2019-10-11
- 1.0.0 — 2019-10-11

## README

# catch-await

[![NPM version][npm-image]][npm-url]
[![Downloads][download-badge]][npm-url]

> Async await wrapper for easy error handling

## 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
yarn add catch-await --save
```

## Usage

```js
import to from 'catch-await';

async function asyncFunction(callback) {
  const { error: userError, data: user } = await to(UserModel.findById(1));
    if(!user) return callback('No user found');
      
    const { error: taskError, data: savedTask } = await to(TaskModel({userId: user.id, name: 'Demo Task'}));
    if(taskError) return callback('Error occurred while saving task');

    if(user.notificationsEnabled) {
       const { error: notificationError } = await to(NotificationService.sendNotification(user.id, 'Task Created'));
       if(notificationError) return callback('Error while sending notification');
    }

    if(savedTask.assignedUser.id !== user.id) {
       const { error: sendError , data: notification } = await to(NotificationService.sendNotification(savedTask.assignedUser.id, 'Task was created for you'));
       if(sendError) return callback('Error while sending notification');
    }

    callback(null, savedTask);
}

async function asyncFunctionWithThrow() {
  const { error, data: user } = await to(UserModel.findById(1));
  if (!user) throw new Error('User not found');
}
```

[npm-url]: https://npmjs.org/package/catch-await
[npm-image]: https://img.shields.io/npm/v/await-to-js.svg?style=flat-square

[download-badge]: http://img.shields.io/npm/dm/await-to-js.svg?style=flat-square

---
_Source: https://npm.io/package/catch-await · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
