[Internal] Intermidiate Coroutine
The loop engine inside Map and Fold
- return_value
- coroutine
- *
Every Map, Fold, Nest, NestWhile, MapIndexed, Comap, Select, and TakeWhile in ComfyUI Functional is really this node wearing a hat. [Internal] Intermidiate Coroutine is the coroutine driver that lets a single ComfyUI execution call a function node over and over - the mechanism that turns your graph into a loop without you writing a line of Python. If the high-order nodes are the friendly face of the pack, this is the engine under the hood, and it's internal for a reason: don't put it on your canvas by hand.
Here's how the trick works. When you run a Map, its base node creates a Python generator (your function body, specialized per element) and hands it to an IntermidiateCoroutine instance via ComfyUI's expand mechanism. The coroutine node then plays a ping-pong game with the generator: it calls coroutine.send(return_value) to feed the previous result back in, the generator replies with (closure, params) describing the next call to build, the node expands that subgraph, and then it chains another instance of itself onto the end of it to handle the next iteration. StopIteration - the generator saying "done" - is what finally returns the accumulated value. It's a lazy, one-node-at-a-time loop, which is why these graphs don't explode into huge node counts the way naive unrolling would.
Inputs: just two wildcards - return_value (the accumulator fed back into the coroutine) and coroutine (the live generator object). Output: one wildcard socket carrying the final value.
There's a safety valve worth knowing about. The node enforces a coroutine execution limit, and if you blow past it you get a RecursionError saying an infinite loop was detected. That's your bug, usually: a NestWhile predicate that never fails or a Fold body that never reaches a base case. If you're deliberately running something long, the pack lets you raise the cap with an environment variable:
export COMFYUI_FUNCTIONAL_COROUTINE_LIMIT=10000
before launching ComfyUI. Don't reach for that to paper over a genuine infinite loop, though - the README's advice about max_depth caps and loop guards is the right fix.
Install
Ships with the one pack:
cd ComfyUI/custom_nodes && git clone https://github.com/Duanyll/comfyui_functional
then restart, or install Duanyll/comfyui_functional from ComfyUI Manager. No models, no pip deps. To see it in action without hand-wiring anything, load example_workflows/functional-recursion.json from the repo - the guarded recursion example is basically this node doing its thing, and it's a great way to build intuition for why loops in ComfyUI need coroutine machinery in the first place.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| return_value | * | — | |
| coroutine | * | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| * | * | — |