1 ---
2 sidebar_label: "no-loops-in-workflow"
3 ---
4
5 export const metadata = {
6 title: `no-loops-in-workflow - ESLint plugin rules`,
7 }
8
9 # {metadata.title}
10
11 This rule disallows `for`, `for-in`, `for-of`, `while`, and `do-while` loops directly inside a `createWorkflow` constructor or a `when().then()` callback.
12
13 ## Severity
14
15 `error`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset.
16
17 ## What it Targets
18
19 This rule targets files in your project's `src/workflows` directory. It reports loops placed directly in a workflow's constructor function or in a `when().then()` callback.
20
21 The following code is reported by the rule:
22
23 ```ts title="src/workflows/example.ts"
24 import {
25 createWorkflow,
26 WorkflowResponse,
27 } from "@medusajs/framework/workflows-sdk"
28
29 createWorkflow("my-workflow", (input) => {
30 for (const item of input.items) {
31 // ...
32 }
33
34 return new WorkflowResponse(input)
35 })
36 ```
37
38 Instead, use `transform` to prepare input data, or loop outside the workflow around the `.run()` call:
39
40 ```ts title="src/workflows/example.ts"
41 import {
42 createWorkflow,
43 transform,
44 WorkflowResponse,
45 } from "@medusajs/framework/workflows-sdk"
46
47 createWorkflow("my-workflow", (input) => {
48 const items = transform({ input }, (data) => {
49 return data.input.items.map((item) => item)
50 })
51
52 return new WorkflowResponse(items)
53 })
54 ```
55
56 ## Why it's Important
57
58 A workflow's constructor only defines the workflow's steps. It runs once when the workflow is created, not every time the workflow executes. So, a loop in the constructor runs at definition time and doesn't behave as you might expect at runtime.
59
60 Learn more in the [Workflow Constraints documentation](!docs!/learn/fundamentals/workflows/constructor-constraints).
61
62 ## Fixable
63
64 This rule isn't auto-fixable.