translation/translated/documents/course/04-workflows/18-understanding-conditiona...

62 lines
1.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 理解条件分支
学习如何创建基于数据条件采用不同路径的工作流,使您的工作流更智能和自适应。
## 什么是条件分支?
条件分支允许工作流:
- **做出决策**:根据数据选择不同的处理路径
- **处理变化**:以不同方式处理不同内容类型
- **优化性能**:为某些输入跳过不必要的步骤
- **自定义行为**:基于条件提供不同体验
## 真实世界示例
想象一个内容处理工作流:
- **短内容**< 50字获得快速处理
- **中等内容**50-200字获得标准处理
- **长内容**> 200字获得带有额外分析的详细处理
## 基本分支语法
```typescript
.branch([
[condition1, step1],
[condition2, step2],
[condition3, step3]
])
```
其中:
- **condition**:返回 `true``false` 的异步函数
- **step**:条件为 `true` 时要执行的步骤
## 条件函数
条件是检查输入数据的函数:
```typescript
// Example condition function
async ({ inputData }) => {
return inputData.wordCount < 50;
};
```
## 多条路径
- 如果多个条件为 `true`**所有匹配的步骤并行运行**
- 如果没有条件为 `true`,工作流继续而不执行任何分支步骤
- 条件按顺序评估,但匹配的步骤同时运行
## 好处
- **智能路由**:将数据发送到最合适的路径
- **性能**:在不需要时跳过昂贵的操作
- **灵活性**:在一个工作流中处理不同场景
- **可维护性**:不同处理路径的清晰逻辑
接下来,您将创建一个带有条件分支的工作流!