Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feats docs(loops) : updated loop over a list of values #1888

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion content/docs/15.how-to-guides/loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ topics:

How to iterate over a list of values in your flow.

In this example, we can use `EachSequential` to iterate over a list of values.
In this guide, you will learn how to iterate over a list of values using the `EachSequential` task. This task enables you to loop through a list of values and execute specific tasks for each value in the list. This approach is useful for scenarios where multiple similar tasks need to be run for different inputs.

## Prerequisites

Before you begin:

- Deploy [Kestra](../02.installation/index.md) in your preferred development environment.
- Ensure you have a [basic understanding of how to run Kestra flows.](../01.getting-started/03.tutorial.md)

## Loop Over Nested Lists of Values

This example demonstrates how to use `EachSequential` to loop over a list of strings and then loop through a nested list for each string. To see the flow in action, define the `each_nested` flow as shown below:

```yaml
id: each_nested
Expand Down Expand Up @@ -40,3 +51,33 @@ tasks:
format: "{{task.id}} > {{outputs['1-2-1_return'].s1['a a'].value}}"
```

Save and execute the `each_nested` flow.

The above flow, when executed, iterates over a nested list of values, logging messages at each level of iteration to track the processing of both the outer and inner list items.

Within the flow:

- `1_each`: Uses the `EachSequential` task to iterate over the list `["s1", "s2", "s3"]`. For each value, it runs the nested tasks defined within.

- `1-1_return`: Logs the task ID, the current list value, and the task run start time.

- `1-2_each`: Iterates over a second list `["a a", "b b"]` and runs a set of tasks for each value in this nested list.

- `1-2-1_return`: Logs the task ID, the nested list value, and the start time of the task run.

- `1-2-2_return`: Logs a custom output from `1-2-1_return`, which shows how to access outputs from previous iterations within the nested loop.

- `1-3_return`: Logs the output from `1-1_return` after the inner loop is completed and displays the corresponding value processed in the outer loop.

- `2_return`: Fetches the output from the nested loop (`1-2-1_return` for the value `a a`) and logs it.


## Next Steps

Now that you've seen how to loop over a list of values using `EachSequential`, you can apply this technique to any scenario where multiple iterations of similar tasks are needed. You can further extend this flow by:
- Adding more complex nested loops.
- Using dynamic input values instead of hardcoded lists.
- Logging or processing additional data from each iteration.

For more advanced use cases, refer to Kestra’s official [EachSequential](https://kestra.io/plugins/core/tasks/flow/io.kestra.plugin.core.flow.eachsequential) task documentation.