ComfyUI Node

ForEachClose

The node that closes an EasyApi for-loop

By lldacing·Created 3 years ago·Updated 8 months ago· 96
ForEachClose
  • flow_control
  • initial_value1
  • initial_value2
  • initial_value3
  • initial_value4
  • initial_value5
  • initial_value6
  • initial_value7
  • initial_value8
  • initial_value9
  • initial_value10
  • initial_value11
  • initial_value12
  • initial_value13
  • initial_value14
  • initial_value15
  • initial_value16
  • initial_value17
  • initial_value18
  • initial_value19
  • value1
  • value2
  • value3
  • value4
  • value5
  • value6
  • value7
  • value8
  • value9
  • value10
  • value11
  • value12
  • value13
  • value14
  • value15
  • value16
  • value17
  • value18
  • value19

ForEachClose is the other half of this pack's loop mechanism - it doesn't do anything meaningful on its own, it only makes sense paired with ForEachOpen. If you found this page by itself, start with the ForEachOpen article: that's where the "why does ComfyUI need a special node pair for looping at all" question gets answered (short version: the graph is a one-directional DAG with no native for loop, and this is one of a few community answers to that real, recurring gap).

This node's job is to receive the flow_control signal from ForEachOpen and, once it does, drive the whole loop: re-trigger everything between Open and Close for the configured number of iterations, and once the last iteration finishes, emit the final state of every tracked value out the other side so the rest of your graph can pick up where the loop left off.

How it works

Wire flow_control from ForEachOpen into ForEachClose - that connection is what defines the loop's boundaries: everything wired between the two nodes is what gets re-executed each pass. The initial_valueN inputs on ForEachClose are how you feed a value back around for the next iteration to see - if you're accumulating something (a growing list, a running count, an image getting progressively edited), you wire the in-loop result of this iteration back into the matching slot here, and it becomes next iteration's starting value on the ForEachOpen side. Once the loop finishes, ForEachClose's valueN outputs carry the final state out to whatever comes after the loop.

The inputs and outputs that matter

  • flow_control (FLOW_CONTROL, required) - the connection back to the matching ForEachOpen. Without this wired, there's no loop.
  • initial_value1 through initial_value19 (any type, all optional) - feed each iteration's updated state back in here if you want it carried to the next pass.
  • Output: value1 through value19 - the final value of each tracked slot once the loop has finished all its iterations, ready to wire into whatever comes next in your graph.

You don't need to use all nineteen slots - only wire up as many as your loop actually needs to track.

How to install it

It ships as part of comfyui-easyapi-nodes, so installing ForEachOpen installs this too. Via ComfyUI Manager, search comfyui-easyapi-nodes. Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/lldacing/comfyui-easyapi-nodes.git
cd comfyui-easyapi-nodes
pip install -r requirements.txt

Restart ComfyUI afterward.

Common issues & troubleshooting

Graph never seems to finish, or seems to run more times than expected. Check the total value set on the matching ForEachOpen - that's what governs iteration count, not anything on this node. ForEachClose just executes what ForEachOpen tells it to.

Final output looks like it's missing the last iteration's changes. Make sure the value you want carried forward is actually wired from inside the loop body back into the matching initial_valueN slot on ForEachClose. If a slot isn't rewired each pass, it just keeps re-emitting its original starting value instead of accumulating anything.

Only some of my tracked values update correctly. Each of the nineteen slots is independent - double check every value you care about has its own consistent slot number on both ForEachOpen and ForEachClose. Mismatched slot numbers between the two nodes is the easiest way to silently lose track of a value mid-loop.

Considering a different loop node entirely. Reasonable - ForEachOpen/Close is one implementation of a pattern several packs attempt in slightly different ways. If your case is genuinely simpler (say, just re-running one sampler N times with a shifting seed), it's worth checking whether a lighter-weight approach fits before committing to the full loop-pair setup.

CategoryEasyApi/Logic

Inputs (20)

NameTypeDefaultDescription
flow_controlFLOW_CONTROL
initial_value1opt*
initial_value2opt*
initial_value3opt*
initial_value4opt*
initial_value5opt*
initial_value6opt*
initial_value7opt*
initial_value8opt*
initial_value9opt*
initial_value10opt*
initial_value11opt*
initial_value12opt*
initial_value13opt*
initial_value14opt*
initial_value15opt*
initial_value16opt*
initial_value17opt*
initial_value18opt*
initial_value19opt*

Outputs (19)

NameTypeDescription
value1*
value2*
value3*
value4*
value5*
value6*
value7*
value8*
value9*
value10*
value11*
value12*
value13*
value14*
value15*
value16*
value17*
value18*
value19*