afterEvent()
The afterEvent() method creates a suspension point in your workflow that waits for a specific event to occur before continuing execution.
SyntaxDirect link to Syntax
workflow.afterEvent(eventName: string): Workflow
ParametersDirect link to Parameters
| Parameter | Type | Description |
|---|---|---|
| eventName | string | The name of the event to wait for. Must match an event defined in the workflow's events configuration. |
Return ValueDirect link to Return Value
Returns the workflow instance for method chaining.
DescriptionDirect link to Description
The afterEvent() method is used to create an automatic suspension point in your workflow that waits for a specific named event. It's essentially a declarative way to define a point where your workflow should pause and wait for an external event to occur.
When you call afterEvent(), Mastra:
- Creates a special step with ID
__eventName_event - This step automatically suspends the workflow execution
- The workflow remains suspended until the specified event is triggered via
resumeWithEvent() - When the event occurs, execution continues with the step following the
afterEvent()call
This method is part of Mastra's event-driven workflow capabilities, allowing you to create workflows that coordinate with external systems or user interactions without manually implementing suspension logic.
Usage NotesDirect link to Usage Notes
- The event specified in
afterEvent()must be defined in the workflow'seventsconfiguration with a schema - The special step created has a predictable ID format:
__eventName_event(e.g.,__approvalReceived_event) - Any step following
afterEvent()can access the event data viacontext.inputData.resumedEvent - Event data is validated against the schema defined for that event when
resumeWithEvent()is called
ExamplesDirect link to Examples
Basic UsageDirect link to Basic Usage
import { LegacyWorkflow } from "@mastra/core/workflows/legacy";
// Define workflow with events
const workflow = new LegacyWorkflow({
name: "approval-workflow",
events: {
approval: {
schema: z.object({
approved: z.boolean(),
approverName: z.string(),
}),
},
},
});
// Build workflow with event suspension point
workflow
.step(submitRequest)
.afterEvent("approval") // Workflow suspends here
.step(processApproval) // This step runs after the event occurs
.commit();