While Task
The while task repeatedly executes a set of steps as long as specified conditions remain true. It is useful for polling, retry logic, or any scenario where the number of iterations is not known in advance.
While Task Structure
Example of a While task step:
activities:
- name: "PollForCompletion"
description: "Poll an external system until the job completes."
variables:
- name: "jobComplete"
value: "false"
steps:
- task: "while"
name: "waitForJob"
conditions:
- expression: "[jobComplete] == 'false'"
maxIterations: 50
steps:
- task: "Http/Get@1"
name: checkStatus
inputs:
url: "{{apiUrl}}/jobs/{{jobId}}/status"
outputs:
- name: jobComplete
mapping: "result.isComplete"
Attributes
- conditions (required) - A list of conditions that are evaluated before each iteration. The loop continues as long as all conditions evaluate to
true. At least one condition must be specified. - steps (required) - The list of task steps to execute on each iteration. Must have at least one step.
- maxIterations (optional) - The maximum number of iterations the While loop will execute. Defaults to
1000. If the loop reaches this limit, it stops and the workflow continues with the next step. This acts as a safety guard to prevent infinite loops from consuming excessive resources. - continueOnError (optional) - A boolean that controls whether the workflow should continue if an error occurs during an iteration. Defaults to
false(the workflow stops on error).
Built-in Variables
During execution, the While task exposes the following variable:
| Variable | Type | Description |
|---|---|---|
iteration | integer | Zero-based index of the current iteration. Removed after the loop completes. |
Best Practices
- Always set
maxIterationswhen you know an upper bound for your use case. The default of 1000 protects against infinite loops, but a tighter limit makes intent clearer and catches logic errors faster. - Update loop variables inside steps so the condition can eventually become
false. A While loop whose condition never changes will run untilmaxIterationsis reached. - Use
continueOnError: truefor retry patterns where occasional failures are expected and should not abort the entire workflow.
Related Topics
- ForEach Task - Iterate over a known collection
- Switch Task - Conditional branching
- Workflow Variables - Variable resolution and scoping