# avait

> Async error handling and fetch without try-catch.

Latest version **2.1.4** (published 2026-07-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install avait
pnpm add avait
yarn add avait
bun add avait
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.1.4 |
| Published | 2026-07-12 |
| First published | 2023-07-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 0 |
| Author | Matthias Giger |
| Maintainers | tobua |
| Keywords | await, asynchronous, fetch |

## Links

- npm: https://www.npmjs.com/package/avait
- Repository: https://github.com/tobua/avait
- Homepage: https://github.com/tobua/avait#readme
- Issues: https://github.com/tobua/avait/issues
- npm.io page: https://npm.io/package/avait

## 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

- 2.1.4 (latest) — 2026-07-12
- 2.1.3 — 2026-07-12
- 2.1.1 — 2024-08-04
- 2.1.0 — 2024-08-04
- 2.0.0 — 2024-08-03
- 1.0.1 — 2024-06-25
- 1.0.0 — 2024-02-03
- 0.7.0 — 2023-09-08
- 0.6.1 — 2023-08-22
- 0.6.0 — 2023-08-18
- 0.5.0 — 2023-08-09
- 0.4.0 — 2023-08-04
- 0.3.0 — 2023-08-03
- 0.2.0 — 2023-08-01
- 0.1.0 — 2023-07-31

## README

<p align="center">
  <img src="https://github.com/tobua/avait/raw/main/logo.png" alt="avait" width="50%">
</p>

# avait

Async error handling and fetch without try-catch.

## Usage

```js
import { it } from 'avait'
import { readFile } from 'fs/promises'

const { error, value } = await it(readFile('./my-file.txt', 'utf-8'))

if (error) return alert('Error')
console.log(`File contents: ${value}`)
```

It's possible to resolve multiple promises in a row.

```js
const { error, title } = await it(fetch('https://dummyjson.com/products/1')).add((next) =>
  next.json(),
)
// title => 'iPhone 9' or similar.
```

## Error Handler

When an error is thrown but the `error` property isn't accessed errors will be sent to any registered error handlers.

```ts
import { it, registerErrorHandler } from 'avait'
import { readFile } from 'fs/promises'

registerErrorHandler((error) => alert(error))

const { value } = await it(readFile('./my-file.txt', 'utf-8'))

console.log(`File contents: ${value}`)
```

## Multiple Promises

It's possible to pass an array of promises. In this case the result `value` as well as the `error` will also be returned as an array. Using the second argument parallelism can be enabled which leads to the promises being run in parallel.

```ts
import { it } from 'avait'

const { value } = await it([firstPromise, secondPromise])
console.log(value[0])
// With parallelism enabled
const { value } = await it([firstPromise, secondPromise], { parallel: true })
console.log(value[1])
```

## Simple `fetch`

This is a super small wrapper around `fetch` that's supposed to make error handling and accessing the data simple.

```ts
import { load } from 'avait'

const { error, status, data, text } = await load('http://localhost:3000/api') // GET method

// error: boolean | string, indicating if the request errored.
// status: The HTTP status code.
// data: parsed JSON data if response is JSON.
// text: string, text content if response contains text.
// ...props: For a JSON response top-level object properties will be spread on the return object.


await load('http://localhost:3000/api', { name: 'John Doe' }) // POST method
await load('http://localhost:3000/api', { id: 1, name: 'John Doe' }) // PUT method
await load('http://localhost:3000/api', 1) // DELETE method
```

## Converting an Async Method to a Synchronous One

> [!IMPORTANT]
> This feature has been removed with Version 2 of this plugin. I haven't found it useful and it currently doensn't work with Bun. Check out [make-synchronized](https://github.com/fisker/make-synchronized) if you're looking to provide a synchronized interface to an asynchronous method.

## Credits

Error handling inspired by [await-to-js](https://github.com/scopsy/await-to-js).

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