- Workflow Functions: Functions that orchestrate/organize steps
- Step Functions: Functions that carry out the actual work
Workflow Functions
Directive:"use workflow"
Workflow functions define the entrypoint of a workflow and organize how step functions are called. This type of function does not have access to the Node.js runtime, and usable npm packages are limited.
Although this may seem limiting initially, this feature is important in order to suspend and accurately resume execution of workflows.
It helps to think of the workflow function less like a full JavaScript runtime and more like “stitching together” various steps using conditionals, loops, try/catch handlers, Promise.all, and other language primitives.
- Runs in a sandboxed environment without full Node.js access
- All step results are persisted to the event log
- Must be deterministic to allow resuming after failures
Math.random and Date constructors are fixed in workflow runs, so you are safe to use them, and the framework ensures that the values don’t change across replays.
Step Functions
Directive:"use step"
Step functions perform the actual work in a workflow and have full runtime access.
- Full Node.js runtime and npm package access
- Automatic retry on errors
- Results persisted for replay
Step functions are primarily meant to be used inside a workflow.
use step directive is a no-op). This means you can reuse step functions in other parts of your codebase without needing to duplicate business logic.
Keep in mind that calling a step function outside of a workflow function will not have retry semantics, nor will it be observable. Additionally, certain workflow-specific functions like
getStepMetadata() will throw an error when used inside a step that’s called outside a workflow.Suspension and Resumption
Workflow functions have the ability to automatically suspend while they wait on asynchronous work. While suspended, the workflow’s state is stored via the event log and no compute resources are used until the workflow resumes execution. There are multiple ways a workflow can suspend:- Waiting on a step function: the workflow yields while the step runs in the step runtime.
- Using
sleep()to pause for some fixed duration. - Awaiting on a promise returned by
createWebhook(), which resumes the workflow when an external system passes data into the workflow.
Writing Workflows
Basic Structure
The simplest workflow consists of a workflow function and one or more step functions.Project Structure
While you can organize workflow and step functions however you like, we find that larger projects benefit from some structure:steps.ts file or separate files within a steps folder. The shared folder is a good place to put common steps that are used by multiple workflows.
Splitting up steps and workflows will also help avoid most bundler related bugs with the Workflow DevKit.