Control Flow
Guide to Budment's native graph control flow structures, including conditional branches, switch cases, fixed loops, and asynchronous polling.
Budment compiles control flow declarations into native graph nodes. Conditional paths and retries are evaluated directly inside the native Go runtime.
Control structures are natively executed by the Go FSM, allowing Budment to automatically track and aggregate execution paths. Every branch taken, match case hit, loop iteration, and poll attempt is inherently measured and pushed to the metrics sink without requiring custom metrics.counter() calls in your JavaScript hooks.
1. Branching: branch
The branch(condition, truePath, falsePath?) node splits execution based on the boolean result of a condition callback.
- Auto-Telemetry: Branch decisions are tracked atomically. The terminal summary outputs the exact hit distribution (e.g.,
branch_1: true 450 | false 550).
2. Multi-Way Matching: match
The match(condition, cases, defaultPath?) node acts as a declarative switch/case construct. It evaluates a condition returning a string or number and natively directs the worker to the matching pipeline.
- Auto-Telemetry: Every matched case (e.g.,
"COMPLETED","PENDING") is recorded by the engine's metrics aggregator automatically.
3. Iteration: loop
The loop(count, logicPath) node executes a sub-pipeline for a fixed number of iterations.
Inside the loop, the engine automatically injects a loop_index variable (a 0-indexed counter) into the worker's memory scope.
import { loop, http } from "@budment/sdk";
export default [
loop(5, [
http.post("https://api.example.com/messages/send").before({
body: {
message: "Ping",
index: "{{loop_index}}",
},
}),
]),
];
Scope Safety in Nested Loops: Budment's Go engine natively tracks memory scope. When entering a loop, it saves the previous state of
loop_index(if any), and perfectly restores or deletes it after the loop completes, preventing variable collisions in nested iterations.
- Auto-Telemetry: Total cycles generated by the loop node are collected instantly in the metrics sink.
4. Asynchronous Polling: poll
The poll(condition, logicPath, policy) node behaves as a native Do-While loop.
When the worker enters this node, it executes the action pipeline first, and only then evaluates the condition predicate. The cycle repeats until the condition returns true or the maxAttempts limit is reached.
- Auto-Telemetry: The engine captures the boolean result of the polling cycle (whether it succeeded or exhausted max attempts) and factors it into performance aggregates automatically.