# wait-for-dependencies

> Manages dependency waiting with cycle detection

Latest version **1.3.0** (published 2017-05-22) · ISC license · 0 weekly downloads

## Install

```sh
npm install wait-for-dependencies
pnpm add wait-for-dependencies
yarn add wait-for-dependencies
bun add wait-for-dependencies
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2017-05-22 |
| First published | 2017-01-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Stewart Mackenzie-Leigh |
| Maintainers | stewartml |
| Keywords | wait, async, promise, cycle, dependency, dependencies |

## Links

- npm: https://www.npmjs.com/package/wait-for-dependencies
- Repository: https://github.com/stewartml/wait-for-dependencies
- Homepage: https://github.com/stewartml/wait-for-dependencies#readme
- Issues: https://github.com/stewartml/wait-for-dependencies/issues
- npm.io page: https://npm.io/package/wait-for-dependencies

## 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.3.0 (latest) — 2017-05-22
- 1.2.0 — 2017-05-22
- 1.1.0 — 2017-04-25
- 1.0.1 — 2017-01-27
- 1.0.0 — 2017-01-27

## README

# wait-for-dependencies

This library provides a class which can be used to wait on dependencies, with
automatic cycle detection.

## Usage

Install:

    $ npm install --save wait-for-dependencies

Import and instantiate:

```js
import WaitFor from 'wait-for-dependencies';

const w = new WaitFor<Function>();

async function task1() {
  await w.waitFor(task1, [task2]);
  // ...do some things
  w.ready(task1);
}

async function task2() {
  // ...do some things
  w.ready(task2);
}

Promise.all([task1(), task2()])
```

In the example above, we define two asynchronous tasks, `task1` and `task2`.
The former has a dependency on the latter, and will not continue until after
`task2` is done.

Note that if `task2` attempted to wait on `task1`, a dependency cycle would be
detected and an exception thrown.

Since the above pattern of waiting on functions is the main use case, there is
a utility class `WaitForFunction`.  Using this, we can rewrite the above as:

```js
import { WaitForFunction, Waiter<Function> } from 'wait-for-dependencies';

const w = new WaitForFunction();

async function task1(wait: Waiter<Function>) {
  await wait(task2);
  // ...do some things
}

async function task2(wait: Waiter<Function>) {
  // ...do some things
}

w.run([task1, task2]);
```

Some of the boilerplate is handled for you.

## API

#### `WaitFor<T>#constructor(stallDetection: StallDetection = StallDetection.throw)`

The `stallDetection` parameter guides what to do if a task waits on an unknown task.
See `map` for details.

#### `map<V>(items: T[], mapFn: (t: T, wait?: Waiter<T>, i?: number) => Promise<V>): Promise<V[]>`

Maps over an array of items.  The `stallDetection` parameter passed to the constructor
guides what to do if a task waits on an unknown task.  That is, for each call to `map`,
if any task waits on an item not given in the `items` parameter, whether to throw
an `Error`, remove the promise from the result value, or not bother trying to detect
such an occurence.

#### `WaitFor<T>#waitFor(id: T, dependencies: T[])`

Waits for the specified list of dependencies.  The waiting task's own ID has to be passed
as the first argument, so that cycle checking can work.

#### `WaitFor<T>#ready(id: T, value?: any)`

Signals that the specified dependency is ready; that is, that anything waiting on it can
now proceed.  The optional `value` parameter allows a value to be passed back as the resolved
value from any `waitFor` calls.

#### `WaitForFunction#run(funcs: Function[], ...args: any[])`

Runs the array of functions, passing each one any `args` and a Waiter as the final argument.

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