|
1
|
--- |
|
2
|
sidebar_label: "no-async-workflow-constructor" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `no-async-workflow-constructor - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule disallows passing an `async` function to `createWorkflow`, since workflow constructors run at definition time and can't await. |
|
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 an `async` function passed as the second argument to `createWorkflow`. |
|
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", async (input) => { |
|
30
|
return new WorkflowResponse(input) |
|
31
|
}) |
|
32
|
``` |
|
33
|
|
|
34
|
Instead, use a synchronous constructor and move asynchronous work into steps: |
|
35
|
|
|
36
|
```ts title="src/workflows/example.ts" |
|
37
|
import { |
|
38
|
createWorkflow, |
|
39
|
WorkflowResponse, |
|
40
|
} from "@medusajs/framework/workflows-sdk" |
|
41
|
|
|
42
|
createWorkflow("my-workflow", (input) => { |
|
43
|
return new WorkflowResponse(input) |
|
44
|
}) |
|
45
|
``` |
|
46
|
|
|
47
|
## Why it's Important |
|
48
|
|
|
49
|
A workflow's constructor only defines the workflow's steps. It runs once at definition time, not on each execution, so it can't await asynchronous work. Use steps for asynchronous operations instead. |
|
50
|
|
|
51
|
Learn more in the [Workflow Constraints documentation](!docs!/learn/fundamentals/workflows/constructor-constraints). |
|
52
|
|
|
53
|
## Fixable |
|
54
|
|
|
55
|
This rule is auto-fixable. Run the linter with the `--fix` option to apply the fix automatically. |