Loop Node
Run a sub-workflow multiple times. This node provides a way to iterate and sequentially run a sub-workflow consisting of 1 or more nodes.
Output of the Loop node is the concatenated output of all iterations of the sub-workflow.
Handles
The Loop node has four handles — two on the left side and two on the right side. Unlike most nodes, the Loop node does not use the standard input/output handles. Instead, it uses a dedicated set of handles to define both the main workflow flow and the loop body.
Left Side
| Handle | Type | Description |
|---|---|---|
| input | Target | Receives data from the preceding node in the main workflow. Connect the previous node's output to this handle. |
| start | Source | Connects to the first node of the loop body (sub-workflow). This is where each iteration begins. |
Right Side
| Handle | Type | Description |
|---|---|---|
| end | Target | Receives the result of each iteration. Connect the last node of the loop body back to this handle. |
| output | Source | Sends the concatenated output of all iterations to the next node in the main workflow. |
Connecting the Loop Node
- Main workflow into the loop: Connect the preceding node to the input handle (left).
- Loop body start: Connect the start handle (left) to the first node of your sub-workflow.
- Loop body end: Connect the last node of your sub-workflow to the end handle (right).
- Main workflow out of the loop: Connect the output handle (right) to the next node in the main workflow.
First Loop Body Node ◀── │ Start End │ ◀── Last Loop Body Node
│ │
Previous Node ──▶ │ Input Output │ ──▶ Next Node
Parameters
initialization
JavaScript expression that returns an object. Evaluated once before the first iteration to define the loop's state variables.
Available variables:
| Variable | Description |
|---|---|
input | The data received by the loop node from the preceding node in the main workflow. |
condition
JavaScript expression evaluated before each iteration. If it evaluates to a truthy value, the sub-workflow runs. If falsy, the loop ends.
Available variables:
| Variable | Description |
|---|---|
input | The data received by the loop node from the preceding node. |
index | The current iteration index (0-based). |
output | The output of the last iteration's terminal node (the node connected to the end handle). Not available on the first check. |
| state variables | All variables defined in initialization are available directly by name (e.g. i, shouldContinue). |
update
JavaScript expression evaluated after each iteration to update the loop's state variables. Must return an object with the same shape as initialization.
Available variables:
| Variable | Description |
|---|---|
input | The data received by the loop node from the preceding node. |
index | The current iteration index (already incremented, i.e. the upcoming iteration number). |
output | The output of the terminal node from the iteration that just completed. |
| state variables | All current state variables are available directly by name. |
Sub-workflow Input
The first node of the loop body (connected to the start handle) receives a loop object as its input with the following properties:
| Property | Description |
|---|---|
loop.input | The data received by the loop node from the preceding node. |
loop.index | The current iteration index (0-based). |
loop.state | An object containing all state variables defined in initialization (with their current values). |
Loop Output
The overall output of the loop node is an array. Each element corresponds to one iteration and has the following shape:
| Property | Description |
|---|---|
input | The original input to the loop node. |
index | The iteration index. |
state | The state variables at the time of that iteration. |
output | The output of the terminal node (connected to the end handle) for that iteration. |
Limits
The loop node allows a maximum of 1000 iterations. If the condition does not become falsy within 1000 iterations, the node will fail.
Examples
Basic Loop with fixed number of iterations
Initialization
{
i: 0;
}
Condition
i < 10;
Update
{
i: i + 1;
}
Loop until a condition is met
This pattern is useful when you don't know the number of iterations in advance. For example, when retrieving items from a paginated data source.
Initialization
{
shouldContinue: true;
}
This condition makes sure loop runs at least once.
Condition
shouldContinue;
Update
{
shouldContinue: output.shouldContinue;
}
Update the `shouldContinue` variable with the output of the sub-workflow. Output of the sub-workflow is output data from node connected to the end handle of the loop node.