# @ziggy42/async-array

> Use async functions in array methods

Latest version **1.0.1** (published 2019-08-05) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @ziggy42/async-array
pnpm add @ziggy42/async-array
yarn add @ziggy42/async-array
bun add @ziggy42/async-array
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2019-08-05 |
| First published | 2019-08-04 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 20.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Andrea Pivetta |
| Maintainers | ziggy42 |
| Keywords | async, array |

## Links

- npm: https://www.npmjs.com/package/@ziggy42/async-array
- Repository: https://github.com/ziggy42/async-array
- Homepage: https://github.com/ziggy42/async-array#readme
- Issues: https://github.com/ziggy42/async-array/issues
- npm.io page: https://npm.io/package/@ziggy42/async-array

## Alternatives

- [@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
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2019-08-05
- 1.0.0 — 2019-08-04

## README

# async-array
`AsyncArray` is a class that inheriths from `Array` and adds `async` methods with support for `async` callbacks such as 
`asyncForEach`, `asyncMap` etc:

```js
const existing = await AsyncArray.of(1, 2, 3)
    .asyncFilter(async (id) => existsInDatabase(id))
```

## Installation
```bash
npm install @ziggy42/async-array
```

## Usage
The constructor of `AsyncArray` behaves exactly like `Array`. From [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array):
> A JavaScript array is initialized with the given elements, except in the case where a single argument is passed to the Array constructor and that argument is a number.  
> If the only argument passed to the Array constructor is an integer between 0 and 232-1 (inclusive), this returns a new JavaScript array with its length property set to that number (Note: this implies an array of arrayLength empty slots, not slots with actual undefined values). If the argument is any other number, a RangeError exception is thrown.

```js
const arr1 = new AsyncArray(1, 2, 3) // AsyncArray [ 1, 2, 3 ]

const arr2 = new AsyncArray(3) // AsyncArray [ <3 empty items> ]
```

There are also two static functions to create new `AsyncArray` instances:
```js
const arr1 = AsyncArray.of(1, 2, 3) // AsyncArray [ 1, 2, 3 ]

const arr2 = AsyncArray.from([1, 2, 3]) // AsyncArray [ 1, 2, 3 ]
```

A regular `Array` can be obtained form an `AsyncArray`:
```js
const arr = AsyncArray.of(1, 2, 3).toArray() // [ 1, 2, 3 ]
``` 

`AsyncArray` adds the following methods:
- `asyncEvery`
- `asyncFilter`
- `asyncFind`
- `asyncFindIndex`
- `asyncFlatMap`
- `asyncForEach`
- `asyncMap`
- `asyncReduce`
- `asyncReduceRight`
- `asyncSome`

These methods behave exactly like the non-async versions but they accept async callbacks.

```js
const existing = await AsyncArray.of('1', '2', '3')
    .asyncFilter(async (id) => existsInDatabase(id))
```

Be aware that the async callbacks are executed in sequence, NOT in parallel.
The following methods allow you to execute the callbacks in parallel instead:
- `asyncFilterAll`
- `asyncMapAll`

```js
const existing = await AsyncArray.of(1, 2, 3)
    .asyncFilterAll(async (id) => existsInDatabase(id))
```

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