Skip to main content
createWhileStep encapsulates a controlled loop: you provide a condition, a mandatory maxIterations, and the step to repeat. The loop chains executions, feeds the previous output as the next input, and collects results.

Polling example

By default the step returns { lastResult, allResults }, with lastResult potentially undefined when no iteration ran. maxIterations is required and throws a WorkflowExecutionError when the condition would otherwise continue.

Condition & safeguards

  • condition({ input, lastOutput, iteration, context, signal }) is evaluated before each iteration. Return false to exit gracefully.
  • maxIterations prevents infinite loops; an explicit error is raised when the limit is reached.
  • The AbortSignal propagates automatically—WorkflowAbortError is thrown on external cancellation.

Prepare input & collect results

  • prepareNextInput is optional. Without it, the initial input is passed to the first iteration, then each output becomes the next input.
  • collect receives { input, results, lastResult, iterations, context } so you can shape the output (aggregation, domain-specific mapping). Without collect, { lastResult, allResults } is returned.

ForEach or While?

  • createForEachStep iterates over a known collection and can parallelise (concurrency).
  • createWhileStep shines when the number of iterations is unknown (polling, refinement loops, validations).
  • Combine the two for advanced pipelines (for example a while that monitors a queue, then a forEach that processes available items).
Need to branch dynamically? See Conditional branches.