Loops are a basic element of control flow in computer programming that Postman Flows supports. As a visual programming environment, Flows has its own design patterns that you need to know to make looping structures work for you.
In Flows, here's the most basic design pattern for a loop:
Begin with a Repeat block. Use a Number block to set the Count
input to the number of times you want Flows to iterate through the loop.
Connect a block that does work (for example, an Evaluate block) to the Repeat block.
Optionally, add more blocks that do work, connecting them together in a series.
Connect a Collect block to the last of the blocks from the previous step.
Connect a Display block to the Collect block.
This loop repeats the specified number of times. There's no way to break out of the loop.
Here's how the loop will look:
Since the For block iterates over a list, you need to provide a list to its input.
Begin with a List block. Alternatively, you can use an HTTP Request block to pass in a list from an API.
Connect a For block to the List block.
Connect a block that does work (for example, an Evaluate block) to the For block.
Optionally, add more blocks that do work, connecting them together in a series.
Connect a Collect block to the last of the blocks from the previous step.
Connect a Display block to the Collect block.
This loop goes all the way through the list - there's no way to break out. Nor can you start in the middle.
Here's how the loop will look:
When planning and creating loop in flow modules, consider the following facts and principles:
Collect blocks are great for synchronizing loops, because the flow module will wait until all data has been collected before continuing.
Don't try to create a variable within a loop. Flow modules treat variables as constants that can only be set once.
In some cases you might need to use a double For loop, similar to the nested loops common in programming languages like Python or JavaScript. If a value in the outer loop needs to be used in processing values in the inner loop, use TypeScript in an Evaluate block to map the outer loop value to every item you're sending through to the inner loop.
Last modified: 2024/03/15