# task-graph-runner

> Run async tasks with dependencies

Latest version **1.0.3** (published 2019-12-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install task-graph-runner
pnpm add task-graph-runner
yarn add task-graph-runner
bun add task-graph-runner
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2019-12-16 |
| First published | 2017-12-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/task-graph-runner) |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 6.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 38 |
| Author | James Kyle |
| Maintainers | thejameskyle |
| Keywords | graph, adjacency, list, tasks, priority, priorities, sort, dependencies, topological, topo, sequencer, task, runner |

## Links

- npm: https://www.npmjs.com/package/task-graph-runner
- Repository: https://github.com/thejameskyle/task-graph-runner
- Homepage: https://github.com/thejameskyle/task-graph-runner#readme
- Issues: https://github.com/thejameskyle/task-graph-runner/issues
- npm.io page: https://npm.io/package/task-graph-runner

## Dependencies (1)

- [array-includes](https://npm.io/package/array-includes.md) ^3.0.3

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

- 1.0.3 (latest) — 2019-12-16
- 1.0.2 — 2018-03-08
- 1.0.1 — 2017-12-27
- 1.0.0 — 2017-12-27

## README

# task-graph-runner

> Run async tasks with dependencies

## Install

```sh
yarn add task-graph-runner
```

## Usage

```js
import taskGraphRunner from 'task-graph-runner';

let graph = new Map([
  ["task-a", ["task-d"]], // task-a depends on task-d
  ["task-b", ["task-d", "task-a"]],
  ["task-c", ["task-d"]],
  ["task-d", []],
]);

async function task(name) {
  console.log(`start ${name}`);
  let result = await exec(name);
  console.log(`end ${name}`);
  return result;
}

let results = taskGraphRunner({ graph, task });
// { safe: false,
//   values: Map { "task-a" => "result-a", "task-b" => "result-b", ... } }
```

Tasks will wait for their dependencies to run, but will be run with maximum
concurrency:

```
start task-d
end task-d
start task-a
start task-c
end task-a
start task-b
end task-c
end task-b
```

#### Resolving cycles

If there are any cycles of dependencies (task-a depends on task-b which depends
on task-a), then `taskGraphRunner` will error unless `force: true` is passed:

```js
let results = taskGraphRunner({ graph, task, force: true });
```

Graph cycles are resolved by picking a single item from the graph which has yet
to be run that has:

1. The fewest number of remaining dependencies (to reduce risk of missing dependencies)
2. The highest number of remaining dependents (to increase chance of unblocking dependents)

## API

```js
declare function taskGraphRunner<Item, Result>({
  graph: Map<Item, Array<Item>>,
  task: (item: Item) => Result,
  force?: boolean,
}): { safe: boolean, values: Map<Item, Result> };
```

#### `opts.graph`

This is a map of items to their dependencies. Items can be any type as long as
they are `===` to one another.

```js
opts.graph = new Map([
  [1, [2, 3]],
  [2, [4]],
  [3, [4]],
  [4, []],
])
```

#### `opts.task`

This function gets called on every item in the graph. It should return a
promise.

```js
opts.task = async function task(item) {
  // ...
};
```

### `opts.force` (default `false`)

`taskGraphRunner` will error if it detects a cycle unless `opts.force` is
`true` in which case it will try to break cycles by choosing a remaining item
in the graph.

If it does detect a cycle, it will cause `res.safe` to be `false`.

### `res.values`

This is a map of the items in the graph to their results from `opts.task`.

```js
res.values
// Map { "task-a" => "result-a", "task-b" => "result-b", ... }
```

### `res.safe`

If the graph ran without any cycles `res.safe` will be `true`, otherwise it
will be `false`.

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