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 canceledThe following code shows the syntax for the branch expression.
expression0
branch:
slow-expression
mid-expression
fast-expression
expression1The 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 | |
Invocation time of the | Immediate |
Requirements for | The |
What the | The body of the |
When the | The |
When the next expression after | Any expression that follows the |
Result of the | A |
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.