docs: document when-then name
This commit is contained in:
@@ -8,19 +8,17 @@ 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 its 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.
|
||||
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 satisified.
|
||||
|
||||
---
|
||||
|
||||
## What is the When-Then Utility?
|
||||
## How to use When-Then?
|
||||
|
||||
when-then from the Workflows SDK executes an action if a condition is satisfied. The `when` function accepts as a parameter a function that returns a boolean value, and the `then` function is chained to `when`. `then` accepts as a parameter a function that's executed if `when`'s parameter function returns a `true` value.
|
||||
The Workflows SDK provides a `when` function that is used to check whether a condition is true. You chain a `then` function to `when` that specifies the steps to execute if the condition in `when` is satisfied.
|
||||
|
||||
For example:
|
||||
|
||||
@@ -77,4 +75,101 @@ In this code snippet, you execute the `isActiveStep` only if the `input.is_activ
|
||||
|
||||
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.
|
||||
|
||||
---
|
||||
|
||||
## Implementing If-Else with When-Then
|
||||
|
||||
when-then doesn't support if-else conditions. Instead, use two `when-then` conditions in your workflow.
|
||||
|
||||
For example:
|
||||
|
||||
export const ifElseHighlights = [
|
||||
["7", "when", "This when-then block acts as an if condition."],
|
||||
["16", "when", "This when-then block acts as an else condiiton."]
|
||||
]
|
||||
|
||||
```ts highlights={ifElseHighlights}
|
||||
const workflow = createWorkflow(
|
||||
"workflow",
|
||||
function (input: {
|
||||
is_active: boolean
|
||||
}) {
|
||||
|
||||
const isActiveResult = when(
|
||||
input,
|
||||
(input) => {
|
||||
return input.is_active
|
||||
}
|
||||
).then(() => {
|
||||
return isActiveStep()
|
||||
})
|
||||
|
||||
const notIsActiveResult = when(
|
||||
input,
|
||||
(input) => {
|
||||
return input.is_active
|
||||
}
|
||||
).then(() => {
|
||||
return notIsActiveStep()
|
||||
})
|
||||
|
||||
// ...
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
In the above workflow, you use two `when-then` blocks. The first one performs a step if `input.is_active` is `true`, and the second performs a step if `input.is_active` is `false`, acting as an else condition.
|
||||
|
||||
---
|
||||
|
||||
## Specify Name for When-Then
|
||||
|
||||
Internally, `when-then` blocks have a unique name similar to a step. When you return a step's result in a `when-then` block, the block's name is derived from the step's name. For example:
|
||||
|
||||
```ts
|
||||
const isActiveResult = when(
|
||||
input,
|
||||
(input) => {
|
||||
return input.is_active
|
||||
}
|
||||
).then(() => {
|
||||
return isActiveStep()
|
||||
})
|
||||
```
|
||||
|
||||
This `when-then` block's internal name will be `when-then-is-active`, where `is-active` is the step's name.
|
||||
|
||||
However, if you need to return in your `when-then` block something other than a step's result, you need to specify a unique step name for that block. Otherwise, Medusa will generate a random name for it which can cause unexpected errors in production.
|
||||
|
||||
You pass a name for `when-then` as a first parameter of `when`, whose signature can accept three parameters in this case. For example:
|
||||
|
||||
export const nameHighlights = [
|
||||
["2", `"check-is-active"`, "The when-then block's name."],
|
||||
["10", "return", "`then` returns a value other than the step's result."]
|
||||
]
|
||||
|
||||
```ts highlights={nameHighlights}
|
||||
const { isActive } = when(
|
||||
"check-is-active",
|
||||
input,
|
||||
(input) => {
|
||||
return input.is_active
|
||||
}
|
||||
).then(() => {
|
||||
const isActive = isActiveStep()
|
||||
|
||||
return {
|
||||
isActive
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Since `then` returns a value different than the step's result, you pass to the `when` function the following parameters:
|
||||
|
||||
1. A unique name to be assigned to the `when-then` block.
|
||||
2. Either an object or the workflow's input. This data is passed as a parameter to the function in `when`'s second parameter.
|
||||
3. A function that returns a boolean indicating whether to execute the action in `then`.
|
||||
|
||||
The second and third parameters are the same as the parameters you previously passed to `when`.
|
||||
|
||||
@@ -157,6 +157,8 @@ const myWorkflow = createWorkflow(
|
||||
})
|
||||
```
|
||||
|
||||
You can also pair multiple `when-then` blocks to implement an `if-else` condition as explained in [this chapter](../conditions/page.mdx).
|
||||
|
||||
### No Conditional Operators
|
||||
|
||||
You can't use conditional operators in a workflow, such as `??` or `||`.
|
||||
|
||||
Reference in New Issue
Block a user