# async-dependency-graph

> A dependency graph that can traverse asynchronous nodes.

Latest version **2.0.2** (published 2026-01-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install async-dependency-graph
pnpm add async-dependency-graph
yarn add async-dependency-graph
bun add async-dependency-graph
```

## Health

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

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

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2026-01-14 |
| First published | 2018-05-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 39.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 9 |
| Author | Chris Woodle |
| Maintainers | chriswoodle |
| Keywords | graph, async, dependency, tree |

## Links

- npm: https://www.npmjs.com/package/async-dependency-graph
- Repository: https://github.com/chriswoodle/async-dependency-graph
- npm.io page: https://npm.io/package/async-dependency-graph

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 2.0.2 (latest) — 2026-01-14
- 2.0.1 — 2026-01-14
- 1.1.5 — 2021-06-21
- 1.1.4 — 2020-07-18
- 1.1.3 — 2020-04-17
- 1.1.2 — 2019-05-29
- 1.1.1 — 2019-01-09
- 1.1.0 — 2018-09-30
- 1.0.5 — 2018-09-28
- 1.0.4 — 2018-09-19
- 1.0.3 — 2018-05-31
- 1.0.2 — 2018-05-30
- 1.0.1 — 2018-05-30
- 1.0.0 — 2018-05-30

## README

# Async Dependency Graph

[![NPM](https://nodei.co/npm/async-dependency-graph.png)](https://www.npmjs.com/package/async-dependency-graph)

![node](https://img.shields.io/npm/l/async-dependency-graph.svg)

A dependency graph that can traverse asynchronous nodes.

## About

Designed for requesting dependent data in web apps. Each node can contain an async function or promise that makes some asynchronous operation, like an http request.

> Inspired by https://github.com/jriecken/dependency-graph

# Install

```shell
yarn add async-dependency-graph 
```

# Usage

```ts
import { Graph, Node } from 'async-dependency-graph';

// Define async functions for each node
const fetchDataA = async () => {
    // Some async operation, e.g., http request
    return 'some data a';
};

const fetchDataB = async () => {
    // ...
    return 'some data b';
};

const fetchDataC = async () => {
    // ...
    return 'some data c';
};

const fetchDataD = async () => {
    // ...
    return 'some data d';
};

const graph = new Graph();

// Create nodes with their async functions
const a = new Node(fetchDataA, { name: 'a' });
const b = new Node(fetchDataB, { name: 'b' });
const c = new Node(fetchDataC, { name: 'c' });
const d = new Node(fetchDataD, { name: 'd' });

// Define dependencies: b depends on a, d depends on c, c depends on a
/**
 * Graph structure: a, b: [a], c: [a], d: [c]
 * (b and c depend on a, d depends on c)
 */
graph.addDependency(b, a); // b depends on a
graph.addDependency(d, c); // d depends on c
graph.addDependency(c, a); // c depends on a

// Traverse the graph - nodes complete in order of dependence, in parallel when possible
await graph.traverse();
        
// Get the resolved data
const dataA = await a.data();
const dataB = await b.data();
const dataC = await c.data();
const dataD = await d.data();
        
console.log(dataA, dataB, dataC, dataD);
```

### Accessing Child Node Data

Parent nodes can access their dependency (child) node data:

```ts
const child = new Node(async () => {
    return { value: 42 };
}, { name: 'child' });

const parent = new Node(async () => {
    // Access child's data
    const childData = await child.data();
    return {
        parentValue: 100,
        childValue: childData.value
    };
}, { name: 'parent' });

graph.addDependency(parent, child);
await graph.traverse();

const parentData = await parent.data();
console.log(parentData); // { parentValue: 100, childValue: 42 }
```

## API Overview

### Graph

- `addDependency(from: Node, to: Node)` - Adds a dependency where `from` depends on `to`.
- `removeDependency(from: Node, to: Node)` - Removes a dependency relationship.
- `removeNode(node: Node)` - Removes a node and all its dependencies from the graph.
- `traverse()` - Traverses the graph, executing nodes in dependency order.
- `dependenciesOf(node: Node)` - Returns an array of nodes that the given node depends on.
- `dependentsOf(node: Node)` - Returns an array of nodes that depend on the given node.
- `hasNode(node: Node)` - Checks if a node exists in the graph.
- `size` - Returns the number of nodes in the graph.
- `reset()` - Resets all nodes in the graph.
- `clearNodeAndDependents(node: Node)` - Clears a node and all its dependents.

### Node

- `new Node(promise: () => Promise<any>, options?: NodeOptions)` - Creates a new node with an async function.
- `data()` - Returns a Promise that resolves when the node's data is ready.
- `setData(data: any)` - Sets data directly on the node.
- `reset()` - Resets the node, clearing its data and mutex.
- `hasData()` - Returns true if the node has data.
- `clearData()` - Clears the node's data.
- `clearMutex()` - Clears the node's mutex.

# Contributing

PR's welcome.

## Setup
```shell
yarn install
# for vscode
yarn dlx @yarnpkg/sdks vscode
```

## Building

```shell
yarn build
```

## Testing

```shell
yarn test
```

# License

MIT

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