Control Flow
Control flow describes how an FQL script determines which expressions are evaluated, how many times they are evaluated, and when evaluation proceeds.
Most FQL code is evaluated in the order it is written. Control-flow constructs change that linear execution model by introducing branching, iteration, delegation to host values, or suspension until a condition is met.
FQL is expression-oriented, so most control-flow constructs produce values. Their results can be returned, assigned to variables, passed to functions, or composed with other expressions. Statement-only forms, such as a returnless braced for, run for effects without producing a value.
Branching with match
A match expression is FQL’s primary structured branching construct. It selects one branch from a set of alternatives, and only that branch is evaluated.
Use match for structured cases or condition chains. Use the ternary operator for a compact two-way value selection.
See Match Expressions.
Iteration with for
A for loop evaluates its body once for each item in a source collection. It is the primary construct for iterating over arrays, query results, and other iterable values.
With a loop-owned return, the result is the collection of values returned by the body. A braced loop may omit that return when it is used as a statement for side effects:
The returnless form produces no collection and cannot be used in a value position.
See For Loops.
Composition with subqueries
A subquery wraps a collecting for expression in parentheses, allowing its array result to be used as a regular value. The loop must have its own return.
Subqueries are useful when an intermediate collection needs to be assigned, returned, passed to a function, or embedded inside another expression.
See Subquery Expressions.
Delegation with query
A query expression delegates query execution to a value that supports a query capability. The query itself may use another language or selector syntax, such as CSS.
The runtime does not interpret the query payload directly. Instead, it passes the query to the target value and returns the result produced by that value.
See Query Expressions.
Coordination with dispatch and waitfor
dispatch sends an event or action to a target value. waitfor suspends evaluation until an event is observed, a value becomes available, or a condition becomes true.
These constructs are commonly used when FQL interacts with external systems, such as browser pages, dynamic documents, or event-driven runtimes.
See Dispatch Expressions and Waitfor Expressions.
Recovering from errors
A recovery tail attaches to an expression and tells the runtime what to do when it fails — return a fallback value, retry the operation, or propagate the error explicitly. The optional chaining operator (?.) handles the common case of accessing a member on a value that may be none.
Recovery tails work on function calls, query, dispatch, waitfor, and grouped expressions. waitfor additionally supports on timeout for timeout-specific recovery.
See Error Handling.
Language constructs and host capabilities
Some control-flow constructs are part of the language itself. match, for, and subqueries are always available and operate on ordinary FQL values.
Other constructs depend on host capabilities. query, dispatch, and waitfor event require the target value to provide the corresponding runtime behavior. For example, a document may support querying, a browser element may support dispatching events, and a page may support observing navigation or network events.
waitfor condition mode is different: it evaluates an ordinary expression repeatedly until the condition succeeds or the timeout is reached. It does not require the target value to provide an event capability.
If a value does not support the capability required by a control-flow construct, evaluation fails at runtime.
See Value Capabilities and Host Values.