Skip to main content

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:

while (true) {

// run my steps

}

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:

(steps, context) => {
return whileloop.iteration < 3;
}

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:

while (...) {
while (...) {
while (...) {
// steps
}
}
}

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:

while(...) {
callAnotherProcess();
}

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:


steps.while_step_ref.output.iteration

OR


{{ steps.while_step_ref.output.iteration }}

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:


// In a Javascript step inside the while loop:
let currentSum = steps.__self?.output?.sum || 0;
currentSum += 10;
return {
sum: currentSum
};

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.

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