Skip to content

๐Ÿ”€ Flow Choreography โ€‹

Define complex multi-step workflows as a Directed Acyclic Graph (DAG). The engine automatically parallelizes, sequences, and handles partial failures.
Directed Acyclic Graph (DAG) Execution
validateCart
checkStock
applyDiscount
calcTax
chargePayment

The Concept โ€‹

Complex workflows like checkout, onboarding, or data pipelines involve multiple dependent steps. Choreography lets you declare the dependency graph and the engine handles the rest.

Quick Start โ€‹

ts
import { choreograph } from "@asyncflowstate/core";

const checkoutFlow = choreograph({
  name: "checkout",
  steps: {
    validateCart: { action: validateCart },
    checkStock: { action: checkStock, dependsOn: ["validateCart"] },
    applyDiscounts: { action: applyDiscounts, dependsOn: ["validateCart"] },
    calculateTax: {
      action: calculateTax,
      dependsOn: ["checkStock", "applyDiscounts"],
    },
    chargePayment: { action: chargePayment, dependsOn: ["calculateTax"] },
    sendReceipt: { action: sendEmail, dependsOn: ["chargePayment"] },
  },
  onStepComplete: (step, result) => updateProgressUI(step),
  rollbackStrategy: "cascade",
});

await checkoutFlow.execute(cartData);

Auto-Parallelization โ€‹

The engine uses Kahn's algorithm (topological sort) to determine execution layers:

mermaid
graph TD
    A["validateCart"] --> B["checkStock"]
    A --> C["applyDiscounts"]
    B --> D["calculateTax"]
    C --> D
    D --> E["chargePayment"]
    E --> F["sendReceipt"]

Steps in the same layer (e.g., checkStock and applyDiscounts) run in parallel.

Progress Tracking โ€‹

ts
choreography.subscribe((state) => {
  console.log(`Progress: ${state.progress}%`);
  console.log("Step statuses:", state.steps);
  // { validateCart: { status: 'done' }, checkStock: { status: 'running' }, ... }
});

Cascade Rollback โ€‹

When rollbackStrategy: 'cascade' is set and a step fails, the engine walks the DAG backwards and calls compensating actions for all completed steps.

Mermaid Export โ€‹

ts
const diagram = checkoutFlow.toMermaid();
// Outputs a valid Mermaid diagram of the DAG

API Reference โ€‹

choreograph(options) โ€‹

OptionTypeDescription
namestringName for debugging
stepsRecord<string, ChoreographyStep>Step definitions
onStepComplete(name, result) => voidPer-step completion callback
onStepError(name, error) => voidPer-step error callback
rollbackStrategy'cascade' | 'none'What to do on failure

ChoreographyStep โ€‹

OptionTypeDescription
actionFlowActionThe async action to execute
dependsOnstring[]Steps that must complete first
optionalbooleanIf true, failure won't stop the pipeline
timeoutnumberTimeout for this step (ms)

Built with by AsyncFlowState Contributors
Open Source ยท MIT License