Unmeshed Logo

References

While Step

The While Step is a container Step that executes exactly one child Step multiple times until the provided javascript based condition returns false. This is particularly handy when you want to apply the same operation (e.g., fetching data, transforming inputs, sending requests) for multiple items.

Just like any programming language:

javascript

The script configured in while loop is a standard JAVASCRIPT script which means you can run complex functions that at the end returns either a boolean true or false

If true the steps will execute and if false the loop will exit.

This is a sample while condition:

javascript

Keywords in Script

FieldDescription
whileloopSpecially injected keyword to access the context of the loop.
iterationAccessible via whileloop.iteration - a zero indexed counter showing the current loop execution count

Nesting While Loops

While loops in Unmeshed support deep nesting, meaning you can run a while loop inside another while loop, and so on:

javascript

If you need to run multiple steps within a single iteration, you can wrap them in a LIST step (which is effectively a container for multiple steps). That way, you still have a single child step from the viewpoint of the While step, but inside that child, you can have as many steps as you need.

Using Sub Processes

You can also invoke another process as part of the While loop:

javascript

This enables you to encapsulate complex logic or separate workflows into a sub-process and repeatedly call it until your loop condition is no longer satisfied.

Loop Context and State

Inside the loop, you'll often want to access or update state as you iterate. Unmeshed offers a built-in keyword whileloop with a property iteration that you can reference in your steps:

javascript

OR

javascript

Updating State

Beyond using the built-in iteration counter, you may have your own state that you want to update or carry through the loop. You can do this with any step that allows writing output, such as a NOOP or JavaScript step. In these steps, you can read the current state, modify it, and write it back so subsequent loop iterations can see the updated values.

For example, you might do something like this:

javascript
Tip

__self refers to the currently running step

That output can then be referenced by the next iteration or by the same iteration’s subsequent steps.

Tip

You can also use Unmeshed State Management to manage state at the process level.

Getting Iteration Outputs

When a step runs inside a While loop, it executes once per iteration. Each run produces an output, but the step's main output field always holds the latest iteration only. Earlier iteration outputs are stored on the step's executionList, while the most recent output is only available on the top-level output.

This means:

  • steps.step_inside_while_ref.output is the most recent output.
  • steps.step_inside_while_ref.executionList includes entries for each run, but the latest run output is only available on the top-level output.

If you need every iteration output as a single array, add a separate JavaScript step after the While loop and collect them explicitly. This is uncommon, but when you need it, this is the reliable approach.

javascript

This pattern keeps your loop fast and simple while still giving you access to every iteration result when needed.

Important

The previous iteration output is available to the step on the next run. In a scripting step, you can use that output to carry incremental state forward, as shown in Updating State.

Summary

  • While loops execute a single child step repeatedly until your JavaScript condition returns false
  • You have access to whileloop.iteration, which tracks the number of times the loop has run so far (zero-indexed)
  • Nesting is fully supported (you can have as many levels of While loops as needed)
  • If you require multiple operations within one iteration, group them inside a List step or a sub process call
  • Use step outputs to manage custom state within your loop, ensuring your loop can read and update these values easily
  • Alternatively use Unmeshed State Management to manage state at the process level
Warning
Avoid running endless loops