docs: general fixes and improvements (#13417)
This commit is contained in:
1 parent
55a35e4721
commit
8785fb4e1a
5 files changed
+69
-31
No files matched your search
@@ -8,11 +8,11 @@ In this chapter, you'll learn how to execute an action based on a condition in a
|
||||
|
||||
## Why If-Conditions Aren't Allowed in Workflows?
|
||||
|
||||
Medusa creates an internal representation of the workflow definition you pass to `createWorkflow` to track and store its steps. At that point, variables in the workflow don't have any values. They only do when you execute the workflow.
|
||||
Medusa creates an internal representation of the workflow definition you pass to `createWorkflow` to track and store the workflow's steps. At that point, variables in the workflow don't have any values. They only do when you execute the workflow.
|
||||
|
||||
So, you can't use an if-condition that checks a variable's value, as the condition will be evaluated when Medusa creates the internal representation of the workflow, rather than during execution.
|
||||
|
||||
Instead, use when-then from the Workflows SDK. It allows you to perform steps in a workflow only if a condition that you specify is satisfied.
|
||||
Instead, use `when-then` from the Workflows SDK. It allows you to perform steps in a workflow only if a condition that you specify is satisfied.
|
||||
|
||||
<Note title="Tip">
|
||||
|
||||
@@ -72,22 +72,29 @@ In this code snippet, you execute the `isActiveStep` only if the `input.is_activ
|
||||
|
||||
### When Parameters
|
||||
|
||||
`when` accepts the following parameters:
|
||||
`when` accepts two parameters:
|
||||
|
||||
1. The first parameter is either an object or the workflow's input. This data is passed as a parameter to the function in `when`'s second parameter.
|
||||
2. The second parameter is a function that returns a boolean indicating whether to execute the action in `then`.
|
||||
- You can perform conditional checks or use conditional operators in this function, but only on the function's parameters.
|
||||
|
||||
### Then Parameters
|
||||
### Then Callback Function
|
||||
|
||||
To specify the action to perform if the condition is satisfied, chain a `then` function to `when` and pass it a callback function.
|
||||
To specify the action to perform if the condition is satisfied, chain a `then` function to `when` and pass it a callback function. The callback function is only executed if `when`'s second parameter function returns a `true` value.
|
||||
|
||||
The callback function is only executed if `when`'s second parameter function returns a `true` value.
|
||||
The callback function passed to `then` is treated the same as the workflow's definition function. That means:
|
||||
|
||||
- You can execute steps in the callback function.
|
||||
- You can't [transform data and variables](../variable-manipulation/page.mdx) in the callback function. Instead, use `transform`.
|
||||
- You can't use if-else conditions in the callback function.
|
||||
- You can't use loops in the callback function.
|
||||
- You should keep in mind the [workflow constructor constraints](../constructor-constraints/page.mdx) in the callback function.
|
||||
|
||||
---
|
||||
|
||||
## Implementing If-Else with When-Then
|
||||
|
||||
when-then doesn't support if-else conditions. Instead, use two `when-then` conditions in your workflow.
|
||||
`when-then` doesn't support if-else conditions. Instead, use two `when-then` conditions in your workflow.
|
||||
|
||||
For example:
|
||||
|
||||
@@ -166,9 +173,15 @@ const { isActive } = when(
|
||||
).then(() => {
|
||||
const isActive = isActiveStep()
|
||||
|
||||
return {
|
||||
const returnData = transform({
|
||||
isActive,
|
||||
}
|
||||
}, (data) => {
|
||||
return {
|
||||
message: data.isActive ? "Active" : "Not Active",
|
||||
}
|
||||
})
|
||||
|
||||
return returnData
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
Reference in new issue
Block a user