A branch
expression starts a block of one or more async subexpressions, and any expression that follows after is executed immediately, without waiting for the branch
expressions to complete.
You can use branch
essentially to treat any async block of code as though it were fire-and-forget immediate, but it still must be called within an async context.
branch:
# This block continues until completed
AsyncFunction1() # Starts effectively the same time as AsyncFunction3()
Method1() # Block can be mixed with immediate expressions
AsyncFunction2()
AsyncFunction3() # Starts effectively the same time as AsyncFunction1()
# If branch block task is still running when AsyncFunction3 completes
# then any remaining branch task is canceled
The following code shows the syntax for the branch
expression.
expression0
branch:
slow-expression
mid-expression
fast-expression
expression1
The diagram below shows the execution flow of the expressions.
It is similar to the unstructured concurrency spawn
expression, but branch
allows for any arbitrary block of code, and is only permissible within, and bounded by, an enclosing async context. Because of this, branch
is preferred over spawn
whenever possible.
Branch Expression Use
Where you can use a branch expression |
Async contexts |
Invocation time of the branch expression |
Immediate |
Requirements for branch code block |
The branch expression must have at least one async expression. |
What the branch expression does |
The body of the branch expression is started as soon as it is encountered. The body of the branch expression continues to evaluate until the code block completes or the enclosing async context completes — whichever occurs first — at which point the branch code block task is canceled. |
When the branch expression completes |
The branch expression completes immediately. |
When the next expression after branch starts |
Any expression that follows the branch expression is started immediately. |
Result of the branch expression |
A branch expression has no result, so its result type is void . |
A branch
expression may not currently be used in the body of an iteration expression such as loop
or for
. If it must be used then wrap it in an async function and have the iteration expression call that function.