# @mighty-maker/async-operators

> Async operators for composable async pipelines

Latest version **0.1.4** (published 2021-03-07) · ISC license · 0 weekly downloads

## Install

```sh
npm install @mighty-maker/async-operators
pnpm add @mighty-maker/async-operators
yarn add @mighty-maker/async-operators
bun add @mighty-maker/async-operators
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.4 |
| Published | 2021-03-07 |
| First published | 2021-01-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Timothy Voice |
| Maintainers | timsvoice |
| Keywords | async, composition |

## Links

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

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

- 0.1.4 (latest) — 2021-03-07
- 0.1.3 — 2021-02-06
- 0.1.2 — 2021-02-06
- 0.1.1 — 2021-01-19
- 0.1.0 — 2021-01-19
- 1.0.2 — 2021-01-18
- 1.0.1 — 2021-01-18
- 1.0.0 — 2021-01-18

## README

# Async Operators

This library contains a collection of utilities useful for composing pipelines of async functions. Many functions in this library have a synchronous analog such as Pipe, Map, and Reduce. These should be familiar and can be used like their synchronous counterparts.

## Installation

```
npm i --save @mighty-maker/async-operators
```

## Usage

### asyncPipe

`asyncPipe` is the backbone of this library and is used to compose a list of async functions, making your program's flow easy to read and reason about. For example:

```javascript
const addOneAsync = async (x) => new Promise((resolve) => setTimeout(() => resolve(x + 1), 500))
const addTwoAsync = async (x) => new Promise((resolve) => setTimeout(() => resolve(x + 2), 500))
const addThree = (x) => x + 3

const pipeline = asyncPipe(
 addOneAsync,
 addTwoAsync,
 addThree
)

await pipeline(1) // 4
```

Here, we are creating a new function `pipeline` that is the result of compose three other functions. `addOneAsync` is applied to the value passed to `pipeline` and `addTwoAsync` is applied to the result of `addOneAsync`. Notice the inclusion of `addThree` in the pipeline. Not every function in the pipeline needs to be async, you can perform synchronous operations at any stage.

### asyncSeries

`asyncSeries` is a bit of an outlier in that it doesn't have a direct analog in JavaScript's built in array operators. Where it's useful is in running a series of async functions where you don't care about the individual results of the function calls, but simply that the series of functions ran. For convenience, `asyncSeries` will return the result of the final function call.

```javascript
 const addTwoAsync = asyncSeries(async (x) => new Promise(resolve => setTimeout(() => resolve(x + 2), 500)))
 const finalResult = await addTwoAsync([1,2,3]) // 5
```

Here the result of calling `addTwoAsync` on the last value in the list (3) is returned (5) even though `addTwoAsync` as applied all the numbers in the list.

Example use case: firing off a series of messages to a service that will process them in the background. Here the return value of the function is irrelevant, we just care that the message was sent without an error.

### asyncMap

`asyncMap` can be thought of as `Array.map()` but performed asynchronously. The results of applying the mapper function you provide to each element in the list are returned as a list.

```javascript
const mapAddTwoAsync = asyncMap(async (x) => new Promise(resolve => setTimeout(() => resolve(x + 2), 500))) 
const resultsList = await mapAddTwoAsync([1,2,3]) // [3, 4, 5]
```

### asyncReduce

`asyncReduce` can be thought of as `Array.reduce()` but performed asynchronously. The results of applying the reducer function to each element in the list are accumulated and returned as the final result.

```javascript
const asyncDouble = async x => new Promise((resolve) => { setTimeout(() => resolve(x + x), 500) })
const asyncDoubleSum = asyncReduce(async (prev, val) => prev + await asyncDouble(val), 0)
 
const accumulatedRestul = await asyncDoubleSum([1,2,3]) // 12
```

`asyncReduce` can be useful for retrieving paginated restults from an API.

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