async
overview
void supports async/await for non-blocking IO operations.
import std.io.net.HttpClient as Http;
import std.io.stdout;
async fn main(args: Array[str]) uint8 {
let http = Http.new();
let res = http.get("api.example.com/data").await?;
stdout.println("status: {}", res.status);
return 0;
}
async functions
any function can be marked async. it returns an Async[T, E] implicitly.
async fn fetch(url: str) Result[Response, str] {
// ...
}
await
.await suspends the current function until the async operation completes.
it unwraps the Async[T, E] into Result[T, E].
combined with ? for concise error propagation:
let res = http.get("example.com").await?;
async type
Async[T, E] = {
status: AsyncStatus,
result: Option[Result[T, E]],
}
AsyncStatus = Pending | Resolved | Failed
model
void async is promise-based, similar to JavaScript. the runtime model (event loop vs thread-per-task) is not yet finalized.