|
1
|
--- |
|
2
|
sidebar_label: "no-nested-when-in-workflow" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `no-nested-when-in-workflow - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule disallows calling `when(...)` inside a `when().then()` callback in a workflow composition function. |
|
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 `when(...)` calls that appear inside the callback of another `when().then()` call within a `createWorkflow` body. |
|
20
|
|
|
21
|
The following code is reported by the rule: |
|
22
|
|
|
23
|
```ts title="src/workflows/example.ts" |
|
24
|
import { |
|
25
|
createWorkflow, |
|
26
|
when, |
|
27
|
WorkflowResponse, |
|
28
|
} from "@medusajs/framework/workflows-sdk" |
|
29
|
|
|
30
|
createWorkflow("my-workflow", (input) => { |
|
31
|
const result = when({ input }, (d) => d.input.items.length > 0).then(() => { |
|
32
|
// Reported: `when` nested inside another `when().then()` callback |
|
33
|
const inner = when({ input }, (d) => d.input.flag).then(() => someStep(input)) |
|
34
|
return inner |
|
35
|
}) |
|
36
|
|
|
37
|
return new WorkflowResponse(result) |
|
38
|
}) |
|
39
|
``` |
|
40
|
|
|
41
|
Instead, restructure the logic into sibling `when(...).then(...)` calls at the same level: |
|
42
|
|
|
43
|
```ts title="src/workflows/example.ts" |
|
44
|
import { |
|
45
|
createWorkflow, |
|
46
|
when, |
|
47
|
WorkflowResponse, |
|
48
|
} from "@medusajs/framework/workflows-sdk" |
|
49
|
|
|
50
|
createWorkflow("my-workflow", (input) => { |
|
51
|
const result = when({ input }, (d) => d.input.items.length > 0).then(() => |
|
52
|
outerStep(input) |
|
53
|
) |
|
54
|
|
|
55
|
const inner = when({ input }, (d) => d.input.items.length > 0 && d.input.flag) |
|
56
|
.then(() => someStep(input)) |
|
57
|
|
|
58
|
return new WorkflowResponse(result) |
|
59
|
}) |
|
60
|
``` |
|
61
|
|
|
62
|
## Why it's Important |
|
63
|
|
|
64
|
`when` tracks its pending condition in a single shared slot rather than a stack. When you nest a `when(...)` call inside a `when().then()` callback, the inner `.then()` clears that shared slot before the outer `.then()` reads it. This causes a `TypeError: Cannot read properties of undefined (reading 'steps')` at server boot rather than at build time, making the bug difficult to trace. |
|
65
|
|
|
66
|
Learn more in the [Conditions in Workflows documentation](!docs!/learn/fundamentals/workflows/conditions). |
|
67
|
|
|
68
|
## Fixable |
|
69
|
|
|
70
|
This rule isn't auto-fixable. |