Skip to content

sargunv/async-task-graph

Repository files navigation

NPM CI Codecov License Node ESM

async-task-graph

This library aids in writing and executing workflows asynchronous tasks with interdependencies, with a focus on well-typed task definitions.

Usage

import { workflowBuilder, concurrentExecutor } from "async-task-graph"

const builder = workflowBuilder<{
  context: { hello: string }
  returns: {
    foo: string
    bar: number
  }
}>()

builder.addTask({
  id: `foo`,
  dependencies: [],
  run: ({ context }) => {
    return Promise.resolve(JSON.stringify(context))
  },
})

builder.addTask({
  id: `bar`,
  dependencies: [`foo`],
  run: ({ getTaskResult }) => {
    const str = getTaskResult(`foo`)
    return Promise.resolve(str.length)
  },
})

const { emitter, runWorkflow } = builder.build(concurrentExecutor())

emitter.on(`taskFinish`, ({ id, result }) => {
  console.log(`${id} returned ${JSON.stringify(result)}`)
})

await runWorkflow({ hello: `world` })

About

Nodejs library to aid in writing type safe async task graphs.

Resources

License

Stars

Watchers

Forks

Packages

No packages published