# await-it

> A wrapper for async/await calls without the need of try/catch block.

Latest version **1.0.2** (published 2022-10-03) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

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

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2022-10-03 |
| First published | 2021-07-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Nenad Novaković |
| Maintainers | dvlden |
| Keywords | async/await, try/catch, wrapper |

## Links

- npm: https://www.npmjs.com/package/await-it
- Repository: https://github.com/dvlden/await-it
- Homepage: https://github.com/dvlden/await-it#readme
- Issues: https://github.com/dvlden/await-it/issues
- npm.io page: https://npm.io/package/await-it

## 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.2 (latest) — 2022-10-03
- 1.0.1 — 2021-10-01
- 1.0.0 — 2021-07-07

## README

# Await it
A wrapper for async/await calls without the need of try/catch block.

If someone told you that you should `try / catch` once at the root of your application and then do your async calls anywhere in the codebase, they are wrong. You should `try / catch` every `async / await` call or use `.then() / .catch()` chainables. Better than callback hell? I think it leads to similar problem...

This module attempts to solve that nesting hell and instead provide clean and fluent api with tuple destructuring.

<br>

### Installation _(pick one)_
- `npm i await-it`
- `yarn add await-it`

<br>

### Usage

We'll pretend that we have some kind of `Promise` already declared globally, to avoid bulky **README** file.

```js
// Fake Global Promises
const fakeResolve = Promise.resolve({ name: 'Nenad Novakovic', age: 28 })
const fakeReject = Promise.reject(new Error('Rejection reason...'))
```

<br>

> Without TypeScript
```js
import { it } from 'await-it'

(async () => {
  const [res, err] = await it(fakeResolve);

  console.log(res) // { name: 'Nenad Novakovic', age: 28 }
  console.log(err) // null
})()

(async () => {
  const [res, err] = await it(fakeReject);

  console.log(res) // undefined
  console.log(err.message) // Rejection reason...
})
```

<br>

> With TypeScript
```ts
import { it } from 'await-it'

(async () => {
  interface User {
    name: string;
    age: number;
  }

  const [res] = await it<User>(fakeResolve);

  console.log(res) // { name: 'Nenad Novakovic', age: 28 }
})()
```

<br>

> Node Environment

```js
// Import as CommonJS
const { it } = require('await-it')

// Import as ESM 
import { it } from 'await-it'
```

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