Documentation

5.8.9. 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/framework/workflows-sdk.

The workflow waits until all steps passed to the parallelize function finish executing before continuing to the next step.

For example:

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

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

It returns an array of the steps' results in the same order they're passed to the parallelize function.

So, prices is the result of createPricesStep, and productSalesChannel is the result of attachProductToSalesChannelStep.

Was this chapter helpful?
Edit this page