4.8.3. Workflow Constraints

This chapter lists constraints of defining a workflow or its steps.

Workflow Constraints#

No Async Functions#

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

Code
1// Don't2const myWorkflow = createWorkflow(3  "hello-world", 4  async function (input: WorkflowInput) {5  // ...6})7
8// Do9const myWorkflow = createWorkflow(10  "hello-world", 11  function (input: WorkflowInput) {12  // ...13})

No Direct Variable Manipulation#

You can’t directly manipulate variables within the workflow's constructor function.

NoteLearn more about why you can't manipulate variables in this chapter

Instead, use the transform utility function imported from @medusajs/framework/workflows-sdk:

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

No If Conditions#

You can't use if-conditions in a workflow.

NoteLearn more about why you can't use if-conditions in this chapter

Instead, use the when-then utility function imported from @medusajs/framework/workflows-sdk:

Code
1// Don't2const myWorkflow = createWorkflow(3  "hello-world", 4  function (input: WorkflowInput) {5  if (input.is_active) {6    // perform an action7  }8})9
10// Do (explained in the next chapter)11const myWorkflow = createWorkflow(12  "hello-world", 13  function (input: WorkflowInput) {14  when(input, (input) => {15    return input.is_active16  })17  .then(() => {18    // perform an action19  })20})

No Conditional Operators#

You can't use conditional operators in a workflow, such as ?? or ||.

NoteLearn more about why you can't use if-conditions in this chapter

Instead, use transform to store the desired value in a variable.

For example:

Code