4.6.1. Workflow Constraints

This chapter lists some constraints to keep in mind when defining a workflow or its steps.

Workflow Constraints#

No Async Functions#

The function passed to the createWorkflow can’t be an async function:

1// Don't2const myWorkflow = createWorkflow<3  WorkflowInput,4  WorkflowOutput5>("hello-world", async function (input) {6  // ...7})8
9// Do10const myWorkflow = createWorkflow<11  WorkflowInput,12  WorkflowOutput13>("hello-world", function (input) {14  // ...15})

No Direct Data Manipulation#

Since the constructor function only defines how the workflow works, you can’t directly manipulate data within the function. Instead, use the transform function:

1// Don't2const myWorkflow = createWorkflow<3  WorkflowInput,4  WorkflowOutput5>("hello-world", function (input) {6  const str1 = step1(input)7  const str2 = step2(input)8
9  return {10    message: `${str1}${str2}`,11  }12})13
14// Do15const myWorkflow = createWorkflow<16  WorkflowInput,17  WorkflowOutput18>("hello-world", function (input) {19  const str1 = step1(input)20  const str2 = step2(input)21
22  const result = transform(23    {24      str1,25      str2,26    },27    (input) => ({28      message: `${input.str1}${input.str2}`,29    })30  )31
32  return result33})

No If Conditions#

You can't use if-conditions in a workflow. Instead, use the when-then utility function explained in the next chapter:

1// Don't2const myWorkflow = createWorkflow<3  WorkflowInput,4  WorkflowOutput5>("hello-world", function (input) {6  if (input.is_active) {7    // perform an action8  }9})10
11// Do (explained in the next chapter)12const myWorkflow = createWorkflow<13  WorkflowInput,14  WorkflowOutput15>("hello-world", function (input) {16  when(input, (input) => {17    return input.is_active18  })19  .then(() => {20    // perform an action21  })22})

Step Constraints#

Returned Values#

A step must only return primitive values or an object. Values of other types, such as Maps, aren't allowed.

1// Don't2import { 3  createStep,4  StepResponse,5} from "@medusajs/workflows-sdk"6
7const step1 = createStep(8  "step-1",9  (input, { container }) => {10    const myMap = new Map()11
12    // ...13
14    return new StepResponse({15      myMap16    })17  }18)19
20// Do21import { 22  createStep,23  StepResponse,24} from "@medusajs/workflows-sdk"25
26const step1 = createStep(27  "step-1",28  (input, { container }) => {29    const myObj: Record<string, unknown> = {}30
31    // ...32
33    return new StepResponse({34      myObj35    })36  }37)
Was this chapter helpful?