Skip to content

Break Task Implementation for Loop Control #1104

Open
@nyamathshaik

Description

@nyamathshaik

What would you like to be added?

Introduce native support for a break task to provide enhanced control within looping constructs (e.g., for). This allows workflow authors to exit the innermost loop early based on a condition or directly.

Proposal(s):

Currently, exiting from a loop prematurely requires workarounds such as manipulating the loop condition externally or using switches and transitions, which makes workflow logic harder to maintain and less readable.

Proposal:

  • Add a native break task type to the Serverless Workflow specification.

  • Support simple unconditional breaks and conditional breaks based on JQ expressions.

  • Allow break tasks to be used within any loop

  • Execution engine should honor the break directive and exit the innermost loop immediately when invoked.

  • Optional: Validate that break can only be used inside looping contexts.

Example I

do:
  - for_loop:
      for:
        in: "[1, 2, 3, 4, 5]"
        each: "item"
      do:
        - check_condition:
            switch:
              - case:
                  when: "$item > 3"
                  then: "break_loop"
              - default:
                  then: "continue_processing"
        - break_loop:
            break: true
        - continue_processing:
            do:
              # other tasks

Example II

do:
  - for_loop:
      for:
        in: "[1, 2, 3, 4, 5]"
        each: "item"
      do:
        - break_loop:
            break:
              condition: "$item > 3"
        - process_item:
            do:
              # your logic here

Alternative(s):

Currently, there is no direct way to exit an inner loop once execution has entered it.

Additional info:

This feature would greatly simplify:

  • Data scanning with early exits

  • Threshold-based batching

  • Custom pagination and retry logic with controlled exits

Let me know if you'd like me to open a PR to help explore this idea further!
@ricardozanini @cdavernas

Community Notes

  • Please vote by adding a 👍 reaction to the feature to help us prioritize.
  • If you are interested to work on this feature, please leave a comment.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions