Async/Await and go {}
Use async fn and prefix await to express dependencies that may suspend. Use go {} to schedule work independently. Channels connect the two styles and provide an explicit way for the caller to wait for results or completion.
A Complete Async Workflow
This example starts an async calculation inside a task and waits for its result before returning from main:
async fn twice(value: i32): i32 {
return value * 2;
}
async fn answer(): i32 {
return (await twice(21)) + 1;
}
fn main(): i32 {
let result = Channel.new<i32>(1);
go {
result.send(await answer());
result.close();
};
return match result.recv() {
Some(value) => {
$println(value);
if value == 43 { 0 } else { 1 }
},
None => 2,
};
}Save as async-result.vx and run vex run async-result.vx. Expected stdout is 43, with exit status 0. These async functions compute immediately; the example demonstrates awaiting and result ownership, not a timer, network I/O or a measured scheduler suspension.
Prefix await
Write await work() rather than work().await. Awaiting a dependency obtains its result before the enclosing expression continues. Parentheses make the order clear in a larger expression:
let first = await left();
let second = await right();
let total = first + second;This fragment is sequential. Two awaited calls are not automatically concurrent; independent progress requires a scheduling operation and a completion protocol. An async { ... } expression constructs a future and captures its inputs; that construction does not itself execute the body or suspend the enclosing task.
go {} Blocks
A go block schedules work. It does not return its block value to the caller. When the caller needs a result, use a channel as in the complete example. When a task is intentionally detached, establish who owns its inputs, how it reports failures and what happens during application shutdown.
Do not teach a program that schedules a task and immediately returns from main as a reliable way to observe that task's output. Explicit completion makes examples and applications independent of incidental shutdown timing.
Suspension and Borrowing
An await can suspend. Values needed after that point must remain valid in the coroutine frame, including the owners behind references. Whether a particular borrow is accepted depends on its origin, storage, lifetime and mutability; there is no general rule that every reference is forbidden across await.
See Async Internals for positive and negative borrow examples. Pinning addresses an address-stability requirement; it does not repair a dangling reference or an exclusivity error.
Channels, Errors and Shutdown
recv() distinguishes a received value from a closed, drained channel. This matters when zero is legitimate data. For fallible work, design a result message with success and error variants and ensure every terminal path notifies the receiver. A task that exits without sending or closing can leave a consumer waiting indefinitely.
Bounded channels introduce backpressure. Decide whether cancellation closes a channel, sends an explicit message, or uses a library's documented cancellation protocol. Then wait for worker completion before releasing shared resources. The fan-in guide shows a multi-worker lifecycle.
When to Use Which
| Need | Mechanism |
|---|---|
| Sequence dependent async operations | async fn and await |
| Start independent work | go {} |
| Transfer results and report completion | Channel<T> |
| Distinguish closure from a zero-valued message | recv() or a documented result API |
| Coordinate multiple producers | Explicit owner of output closure and worker completion |