How can I wait for all data before processing a foreach in n8n?

I’m trying to create a workflow where I need to compare items from two different API calls. The result of these API calls is not the same, and I need to wait for all items from these two API calls before making any comparisons.

The standard code for a “foreach” approach seems to process items as they are available, but I need a way to iterate over all items after these two API calls are done.

1 Like

hello @7jeosi

The simplest solution is to use the Compare Datasets node.
You can also use two HTTP Nodes, one after another (set the “run once” for the second one), and a Code node in “run once for all items” mode, then grab both outputs and perform the compare actions.

Hi @7jeosi Welcome!
The best way is to use merge node along with Aggregate node. Just merge everything along with loop over items and then aggregate. (scalable in production)

Great question @7jeosi you’re right that the default “Run Once for Each Item” approach processes each item as it comes in. In your scenario, where we want to wait for both API calls to finish before we compare, we can use “Run Once for All Items” Mode

Instead of processing items one-by-one, you’ll want to:

  1. Set both API nodes to “Run Once for All Items” (this is the default)

    • This ensures each API call completes fully before moving forward
  2. Use a Merge node to combine the results

    • Type: Merge by Position or Merge by Key depending on your comparison logic

    • This waits for both branches to complete before proceeding

  3. Use a Code node to perform the comparison

    • Access all items from both API calls using $input.all()

    • Perform your comparison logic across all items

Hope this helps!

One more angle worth considering if your two API calls can run in parallel: use two separate HTTP Request nodes that both feed into a Merge node set to “Wait for All Inputs” (merge by position or index). This way n8n won’t proceed past the Merge until both branches have completed — no polling, no artificial waits.

If the two API responses have matching keys you want to join on, set the Merge mode to “Combine > Merge by Key” and specify the key field. Then downstream you get a single unified dataset you can loop over or run a Code node against.

The Compare Datasets node (mentioned above) is perfect once you have both datasets merged — it’ll diff them and output added/removed/changed rows which sounds exactly like what you need for comparisons.

Quick pattern that works well in production:

  • HTTP Request A → (branch 1)
  • HTTP Request B → (branch 2)
  • Both feed into Merge node (“Wait for All Inputs”)
  • Merge output → Compare Datasets or Code node

The Aggregate node is useful if you need to collect all items from a loop back into one item before comparing — different use case but worth knowing about.