|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} Run Workflow Steps in Parallel`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this chapter, you’ll learn how to run workflow steps in parallel. |
|
8
|
|
|
9
|
## parallelize Utility Function |
|
10
|
|
|
11
|
If your workflow has steps that don’t rely on one another’s results, run them in parallel using `parallelize` from the Workflows SDK. |
|
12
|
|
|
13
|
The workflow waits until all steps passed to the `parallelize` function finish executing before continuing to the next step. |
|
14
|
|
|
15
|
For example: |
|
16
|
|
|
17
|
export const highlights = [ |
|
18
|
["22", "[prices, productSalesChannel]", "The result of the steps. `prices` is the result of `createPricesStep`, and `productSalesChannel` is the result of `attachProductToSalesChannelStep`."], |
|
19
|
["22", "parallelize", "Run the steps passed as parameters in parallel."], |
|
20
|
] |
|
21
|
|
|
22
|
```ts highlights={highlights} collapsibleLines="1-12" expandButtonLabel="Show Imports" |
|
23
|
import { |
|
24
|
createWorkflow, |
|
25
|
WorkflowResponse, |
|
26
|
parallelize, |
|
27
|
} from "@medusajs/framework/workflows-sdk" |
|
28
|
import { |
|
29
|
createProductStep, |
|
30
|
getProductStep, |
|
31
|
createPricesStep, |
|
32
|
attachProductToSalesChannelStep, |
|
33
|
} from "./steps" |
|
34
|
|
|
35
|
interface WorkflowInput { |
|
36
|
title: string |
|
37
|
} |
|
38
|
|
|
39
|
const myWorkflow = createWorkflow( |
|
40
|
"my-workflow", |
|
41
|
(input: WorkflowInput) => { |
|
42
|
const product = createProductStep(input) |
|
43
|
|
|
44
|
const [prices, productSalesChannel] = parallelize( |
|
45
|
createPricesStep(product), |
|
46
|
attachProductToSalesChannelStep(product) |
|
47
|
) |
|
48
|
|
|
49
|
const refetchedProduct = getProductStep(product.id) |
|
50
|
|
|
51
|
return new WorkflowResponse(refetchedProduct) |
|
52
|
} |
|
53
|
) |
|
54
|
``` |
|
55
|
|
|
56
|
The `parallelize` function accepts the steps to run in parallel as a parameter. |
|
57
|
|
|
58
|
It returns an array of the steps' results in the same order they're passed to the `parallelize` function. |
|
59
|
|
|
60
|
So, `prices` is the result of `createPricesStep`, and `productSalesChannel` is the result of `attachProductToSalesChannelStep`. |