> ## Documentation Index
> Fetch the complete documentation index at: https://docs.alumio.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Apply in a while loop

> Runs a transformer in a while loop.

export const FormField = ({property, label, required = false, diType, uiPatterns, children}) => {
  const patterns = Array.isArray(uiPatterns) ? uiPatterns : uiPatterns ? [uiPatterns] : [];
  return <div style={{
    margin: '1.5rem 0'
  }}>
      <div style={{
    display: 'flex',
    alignItems: 'baseline',
    flexWrap: 'wrap',
    gap: '0.5rem',
    marginBottom: '0.25rem'
  }}>
        <strong>{label}</strong>
        <span style={{
    fontSize: '0.7rem',
    fontWeight: 600,
    textTransform: 'uppercase',
    letterSpacing: '0.04em',
    color: required ? '#6241f5' : '#6b7280'
  }}>
          {required ? 'Required' : 'Optional'}
        </span>
      </div>

      <div>{children}</div>

      {diType || patterns.length > 0 ? <p style={{
    fontSize: '0.85rem',
    color: '#6b7280',
    marginBottom: 0
  }}>
          {diType ? <>
              See the <a href={`/references/${diType}`}>{diType}</a> reference.{' '}
            </> : null}
          {patterns.length > 0 ? <>
              Accepts a <a href={`/documentation/placeholders-and-patterns/patterns#${patterns[0]}`}>pattern</a>.
            </> : null}
        </p> : null}
    </div>;
};

Repeats one or more transformers over and over for as long as a condition stays true. Before each pass it checks your condition against the current data; while the condition holds it runs the transformers, then checks again. The moment the condition no longer holds, the loop stops and the data continues on its way. This is useful for tasks that need to keep going until something is done, such as following pages of results, draining a queue one item at a time, or repeatedly adjusting a value until it reaches a target.

Because the loop keeps going until the condition turns false, the transformers you run should change the data in a way that will eventually make the condition false. As a safety net there is also a hard limit on the number of passes (see Maximum iterations), which stops the loop even if the condition never becomes false.

This documentation describes: [Schema](/schemas/transformer/while)

## Fields

The fields are spread across three tabs: Condition, Transformers, and Settings.

### Condition

<FormField property="condition" label="Condition" required diType="filter">
  The check that decides whether the loop keeps running. It is evaluated before every pass: while it is true the transformers run again, and as soon as it is false the loop stops. Make sure the transformers below move the data toward the point where this condition becomes false, otherwise the loop only stops once it hits the maximum number of passes.
</FormField>

### Transformers

<FormField property="transformers" label="Transformers" required diType="transformer">
  The transformer or transformers that run on each pass. When you add more than one they run in the order you list them, and the next pass starts from the result of the previous pass.
</FormField>

### Settings

<FormField property="maxIterations" label="Maximum iterations" required>
  A safety cap on how many times the loop may run, even if the condition never becomes false. Once this many passes have happened, the loop stops regardless of the condition. You can set it between 1 and 1000, and it defaults to 100. Keep it high enough for your real workload but low enough to protect against a loop that never ends.
</FormField>

## Sample data

This example uses a simple counter to show the loop in action. The condition keeps running while `counter` is less than `3`, and on each pass the [Apply operator](/references/transformer/operator) step adds `step` (which is `1`) to `counter`. The loop runs three times and then stops once `counter` reaches `3`.

| Field              | Value                                                       |
| ------------------ | ----------------------------------------------------------- |
| Condition          | `counter` is less than `3`                                  |
| Transformers       | Apply operator: add `step` to `counter`, store in `counter` |
| Maximum iterations | `100`                                                       |

Input:

```json theme={null}
{
    "sku": "WS-001",
    "counter": 0,
    "step": 1
}
```

Output:

```json theme={null}
{
    "sku": "WS-001",
    "counter": 3,
    "step": 1
}
```

```json Configuration theme={null}
{
  "prototype": "while",
  "parameters": {
    "maxIterations": 100,
    "condition": {
      "prototype": "value-condition-v2",
      "parameters": {
        "accessor": {
          "prototype": "key",
          "parameters": { "keys": ["counter"] }
        },
        "conditions": [
          {
            "prototype": "number-less-than",
            "parameters": { "value": 3 }
          }
        ]
      }
    },
    "transformers": [
      {
        "prototype": "operator",
        "parameters": {
          "operator": "addition",
          "left": "counter",
          "right": "step",
          "destination": "counter"
        }
      }
    ]
  }
}
```
