Workflow.foreach()
.foreach() 方法创建一个循环,对数组中的每一项执行一个步骤。它总是返回一个数组,包含每次迭代的输出,并保留原来的顺序。
🌐 The .foreach() method creates a loop that executes a step for each item in an array. It always returns an array containing the output from each iteration, preserving the original order.
使用示例Direct link to 使用示例
🌐 Usage example
workflow.foreach(step1, { concurrency: 2 });
参数Direct link to 参数
🌐 Parameters
step:
opts?:
返回Direct link to 返回
🌐 Returns
workflow:
行为Direct link to 行为
🌐 Behavior
执行与等待Direct link to 执行与等待
🌐 Execution and waiting
.foreach() 方法会在下一步执行之前处理所有项目。紧随 .foreach() 的步骤只有在每次迭代完成后才会运行,无论并发设置如何。使用 concurrency: 1(默认),项目按顺序处理。提高并发度时,项目以并行批处理的方式处理,但下一步仍然会等待所有批次完成后才执行。
🌐 The .foreach() method processes all items before the next step executes. The step following .foreach() only runs after every iteration has completed, regardless of concurrency settings. With concurrency: 1 (default), items process sequentially. With higher concurrency, items process in parallel batches, but the next step still waits for all batches to finish.
如果你需要对每个项目执行多个操作,请将嵌套工作流用作步骤。这可以将每个项目的所有操作集中在一起,比多次调用 .foreach() 更清晰。有关示例,请参见 foreach 中的嵌套工作流。
🌐 If you need to run multiple operations per item, use a nested workflow as the step. This keeps all operations for each item together and is cleaner than chaining multiple .foreach() calls. See Nested workflows inside foreach for examples.
输出结构Direct link to 输出结构
🌐 Output structure
.foreach() 总是输出一个数组。输出数组中的每个元素对应于处理输入数组中相同索引的元素的结果。
// Input: [{ value: 1 }, { value: 2 }, { value: 3 }]
// Step adds 10 to each value
// Output: [{ value: 11 }, { value: 12 }, { value: 13 }]
在 .foreach() 之后使用 .then()Direct link to using-then-after-foreach
🌐 Using .then() after .foreach()
当你将 .then() 链接在 .foreach() 之后时,下一步会将整个输出数组作为输入接收。这允许你将所有结果聚合或一起处理。
🌐 When you chain .then() after .foreach(), the next step receives the entire output array as its input. This allows you to aggregate or process all results together.
workflow
.foreach(processItemStep) // Output: array of processed items
.then(aggregateStep) // Input: the entire array
.commit();
在 .foreach() 之后使用 .map()Direct link to using-map-after-foreach
🌐 Using .map() after .foreach()
在将数组输出传递到下一步之前,使用 .map() 对其进行转换:
🌐 Use .map() to transform the array output before passing it to the next step:
workflow
.foreach(processItemStep)
.map(async ({ inputData }) => ({
total: inputData.reduce((sum, item) => sum + item.value, 0),
count: inputData.length
}))
.then(nextStep)
.commit();
链式调用多个 .foreach()Direct link to chaining-multiple-foreach-calls
🌐 Chaining multiple .foreach() calls
当你链式调用 .foreach() 时,每个调用都会操作前一步得到的数组:
🌐 When you chain .foreach() calls, each operates on the array from the previous step:
workflow
.foreach(stepA) // If input is [a, b, c], output is [A, B, C]
.foreach(stepB) // Operates on [A, B, C], output is [A', B', C']
.commit();
如果 .foreach() 内的某个步骤返回一个数组,输出将变为数组的数组。使用 .map() 和 .flat() 来扁平化:
🌐 If a step inside .foreach() returns an array, the output becomes an array of arrays. Use .map() with .flat() to flatten:
workflow
.foreach(chunkStep) // Output: [[chunk1, chunk2], [chunk3, chunk4]]
.map(async ({ inputData }) => inputData.flat()) // Output: [chunk1, chunk2, chunk3, chunk4]
.foreach(embedStep)
.commit();
相关Direct link to 相关
🌐 Related