Skip to content

Workflow Chaining

Workflow chaining lets you connect workflows together. When one workflow finishes, it can automatically trigger another based on the outcome.

Chain properties

Each workflow definition supports three chaining hooks:

PropertyTriggers when
onSuccessThe workflow completes successfully (all steps passed).
onFailureThe workflow fails (a step failed with no onFailure branch).
onCompleteThe workflow finishes, regardless of outcome. Runs after onSuccess or onFailure.

Example

const synkro = await Synkro.start({
transport: "redis",
connectionUrl: "redis://localhost:6379",
workflows: [
{
name: "ProcessOrder",
steps: [
{ type: "ValidateOrder", handler: validateOrder },
{ type: "ChargePayment", handler: chargePayment },
{ type: "FulfillOrder", handler: fulfillOrder },
],
onSuccess: "StartShipment",
onFailure: "HandleError",
onComplete: "NotifyCustomer",
},
{
name: "StartShipment",
steps: [
{ type: "CreateLabel", handler: createLabel },
{ type: "SchedulePickup", handler: schedulePickup },
],
},
{
name: "HandleError",
steps: [
{ type: "LogError", handler: logError },
{ type: "RefundPayment", handler: refundPayment },
],
},
{
name: "NotifyCustomer",
steps: [
{ type: "SendEmail", handler: sendEmail },
],
},
],
});
await synkro.publish("ProcessOrder", { orderId: "abc-123" });

Success path

ProcessOrder completes successfully:

  1. StartShipment is triggered (onSuccess).
  2. NotifyCustomer is triggered (onComplete).

Failure path

ProcessOrder fails at any step:

  1. HandleError is triggered (onFailure).
  2. NotifyCustomer is triggered (onComplete).

Inherited context

Chained workflows inherit the requestId and payload from the parent workflow. The payload includes any modifications made by ctx.setPayload() during the parent workflow’s execution.

Combining with conditional routing

Workflow chaining and conditional routing work together. Conditional routing controls branching within a workflow; chaining controls what happens after a workflow finishes. You can use both on the same workflow.