Ir para o conteúdo

Lição 14 · Flow Control

Merging Data and Looping

Combine branches with the n8n Merge node and its modes, and process items in batches with Loop Over Items, while avoiding infinite loops and unmatched records.

Branches split data; sooner or later you need to bring it back together. You also sometimes need to work through items in small groups. This lesson covers the Merge node and the Loop Over Items node.

Merge modes

Merge joins two or more inputs. Its main modes are:

Mode What it does
Append Stack items from the inputs one after another
Combine Join items by matching fields, by position, or in all combinations
Choose Branch Wait for the inputs and pass one of them through

Combine by matching fields works like a database join. You pick the key in each input, such as customerId. Then you decide what to keep: only matches, everything from one side, or unmatched items. Combine by position pairs item 1 with item 1, item 2 with item 2, and so on. Use it only when you are sure both lists have the same order.

Python Stand-in: join two lists by a key

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In a real workflow, decide what happens to the unmatched order. Silently dropping it may hide a data problem. If you know SQL, this is the difference between an inner and an outer join; see the SQL tutorial.

Merge usually needs data on its inputs. If a branch never runs, check how your version of the node behaves with one empty input, and test that case.

Loops come free (mostly)

Most nodes already repeat over each item, so you often need no loop at all. Ten items into a "create task" node makes ten tasks. You need a loop only when you want to:

  • process items in batches, for example 20 at a time;
  • pause between batches to respect rate limits;
  • repeat steps for a node that does not run per item;
  • repeat until a condition is met.

Loop Over Items

The Loop Over Items node splits incoming items into batches of the size you set. Its loop output runs your steps, which connect back to the node. When all batches have been processed, the done output runs once. You do not need an If node just to end this kind of loop.

flowchart LR
  A["Items"] --> L["Loop Over Items (batch 20)"]
  L -->|loop| B["Call API"]
  B --> W["Wait"]
  W --> L
  L -->|done| D["Summary"]

Each batch calls the API and waits, then the loop continues until nothing is left.

JS Stand-in: batches with a pause counter

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Loops with a condition

You can also build a loop by connecting a node back to an earlier one and using If to decide whether to continue, for example "check status until the job is finished". Always add:

  • a maximum number of attempts;
  • a wait between checks;
  • a clear failure path when the limit is reached.

An endless loop can use up quota, fill your execution history and freeze your instance.

Inside the loop

Expressions inside a loop can behave differently from what you expect, especially when referring to nodes that ran several times. Look at the data for a specific run in the node view, and prefer passing needed values along in items.

Common mistakes

  • Adding a loop when the node already runs per item.
  • Pairing lists by position when their order can differ.
  • Looping until success with no limit.

Choosing a design

Before you add a Merge node, ask whether you really need two branches. Often you can look up extra data in a single chain: for each order, call the customer service, then combine the reply with the order using an expression that reaches back to the earlier node. A Merge makes more sense when you fetch two whole lists once and join them, because that needs far fewer requests than one lookup per record.

Check duplicate keys too. If two customers share the same key, a join may not give the single match you expected. Remove or investigate duplicates before joining. After every join, compare counts: how many came in, how many matched and how many did not. Those three numbers catch many mistakes in minutes.

For batch loops, choose the batch size from the limits of the service you call, not from guesswork. A small batch with a pause is gentle and easy to debug. A large batch is faster, but a failure may affect more records. Note the chosen size and pause in the workflow.

Practice

You have a list of 60 product IDs and an API that accepts at most 10 IDs per request. Design the flow: how do you batch them, where does the pause go and what happens after the final batch? Calculate the number of requests, which is six. Then add a second source with prices and decide which Merge mode joins prices to products. Describe what happens to a product without a price, and where it is reported. Test with a small list first, such as 12 IDs, so you can follow every batch by hand.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. Which Merge mode behaves like a database join on a key?

    It pairs records that share a key value.
  2. 2. When do you need Loop Over Items?

    Most nodes already run per item; loops help with batching.
  3. 3. What must a condition-based loop have?

    A limit prevents an endless loop.

Summary

  • Merge can append, combine by key or position, or choose a branch.
  • Decide what happens to unmatched records.
  • Most nodes loop over items for you; use Loop Over Items for batches.
  • Every custom loop needs a limit and a way out.

Next: Sub-workflows and Waiting

Seu progresso fica salvo neste navegador. Não precisa de conta.