# async-await-control

> Async/await flow control functions

Latest version **0.2.1** (published 2018-02-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install async-await-control
pnpm add async-await-control
yarn add async-await-control
bun add async-await-control
```

## 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.2.1 |
| Published | 2018-02-21 |
| First published | 2018-02-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 15.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Aleš Menzel |
| Maintainers | alesmenzel |
| Keywords | async, await, control, flow, utility |

## Links

- npm: https://www.npmjs.com/package/async-await-control
- npm.io page: https://npm.io/package/async-await-control

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.5

## 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.2.1 (latest) — 2018-02-21
- 0.2.0 — 2018-02-19

## README

# Async/await utility functions

This library provides control flow utility functions for async/await. Since async/await returns promises, you can also use it with plain `.then` and `.catch`.
Includes the following:

* [`paralell`](#parallel) - Run any number of tasks in parallel
* [`parallelLimit`](#parallel) Runs maximum of `{limit}` number of tasks in parallel
* [`waterfall`](#waterfall) - Run tasks one by one, passing down the result of previous task
* [`series`](#series) - Runs tasks in a series

## Instalation

`npm install async-await-control`

or if you use yarn

`yarn add async-await-control`

## Usage

### [Parallel / Parallel limit](#parallel)

Run any number of tasks in parallel

#### Parameters

| Name  |       Type       | Description                                                                                   |
| ----- | :--------------: | :-------------------------------------------------------------------------------------------- |
| tasks | Iterable\|Object | A collection of async functions to run. Each async function must return a Promise or a value. |
| limit |      Number      | (Optional) Limit the number of parallel tasks                                                 |

```javascript
// tasks can be Array/Iterable/Object
const tasks = {
  first: () => Promise.resolve(1),
  second: () => Promise.resolve(2),
  third: () => Promise.resolve(3)
};

const res = await parallel(tasks, 2);
// { first: 1, second: 2, third: 3 }
```

### [Waterfall](#waterfall)

Run tasks one by one, passing down the result of previous task

#### Waterfall

| Name  |   Type   | Description                                                                                                                                                                    |
| ----- | :------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| tasks | Iterable | An array of async functions to run. Each function should complete with any number of result values. The result values will be passed as arguments, in order, to the next task. |

Returns array of results or a single value if only single value is passed in the last iteratee.

```javascript
const tasks = [
  () => Promise.resolve(1),
  (a) => Promise.resolve([1, 2]), // a = 1
  (a, b) => Promise.resolve([3, 4]), // a = 1, b = 2
];

const res = await waterfall(tasks);
// [4, 5]


const tasks = [
  () => Promise.resolve(1),
  (a) => Promise.resolve([1, 2]), // a = 1
  (a, b) => Promise.resolve(3), // a = 1, b = 2
];

const res = await waterfall(tasks);
// 3
```

### [Series](#series)

Runs tasks in a series

#### Series

| Name  |   Type   | Description                                                                           |
| ----- | :------: | :------------------------------------------------------------------------------------ |
| tasks | Iterable | An array of async functions to run. Each function should return a Promise or a value. |

```javascript
const tasks = [
  () => Promise.resolve(1),
  () => Promise.resolve([1, 2]),
  () => Promise.resolve([3, 4]),
];

const res = await waterfall(tasks);
// [1, [1, 2], [3, 4]]
```

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