4.6.6. Run Workflow Steps in Parallel

In this chapter, you’ll learn how to run workflow steps in parallel.

parallelize Utility Function#

If your workflow has steps that don’t rely on one another’s results, run them in parallel using the parallelize utility function imported from the @medusajs/workflows-sdk.

The workflow waits until all steps passed to the parallelize function finish executing before continuing with the rest of its implementation.

For example:

11} from "./steps"12
13interface WorkflowInput {14  title: string15}16
17const myWorkflow = createWorkflow<18  WorkflowInput,19  ProductDTO20>("my-workflow", (input) => {21   const product = createProductStep(input)22
23   const [prices, productSalesChannel] = parallelize(24     createPricesStep(product),25     attachProductToSalesChannelStep(product)26   )27
28   const id = product.id29   return getProductStep(product.id)30 }31)

The parallelize function accepts the steps to run in parallel as a parameter.

It returns an array of the steps' results. The results are ordered based on the parallelize parameters' order.

Was this chapter helpful?