Select & Join
Race two async operations and take the first result, or wait for all of them to complete.
Hew
select and await fork (...) are expressions that return values directly. An after clause places the timeout beside the operations it bounds.
actor Primary {
receive fn fetch(key: string) -> i64 { 1 }
}
actor Fallback {
receive fn fetch(key: string) -> i64 { 2 }
}
actor Db {
receive fn get_users() -> i64 { 10 }
receive fn get_posts() -> i64 { 20 }
}
fn main() {
let primary = spawn Primary;
let fallback = spawn Fallback;
let db = spawn Db;
// Race two sources — first reply wins
let winner = select {
x from primary.fetch("cache-key") => x handle e { -1 },
y from fallback.fetch("cache-key") => y handle e { -1 },
after 100ms => -1,
};
println(winner);
// Wait for both to complete — destructure results
let (users, posts) = await fork (
db.get_users(),
db.get_posts(),
);
println(users.expect("database actor stopped"));
println(posts.expect("database actor stopped"));
}Go
Go's select is a statement over channels, so each operation runs in its own goroutine that writes to a channel. Joining uses result channels or a sync.WaitGroup with shared variables.
// Race: wire up channels and goroutines first
ch1 := make(chan Result, 1)
ch2 := make(chan Result, 1)
go func() { ch1 <- primary.Fetch(key) }()
go func() { ch2 <- fallback.Fetch(key) }()
var winner Result
select {
case x := <-ch1:
winner = x
case y := <-ch2:
winner = y
case <-time.After(100 * time.Millisecond):
winner = Result{Err: "timeout"}
}
// Join: buffered channels collect results
usersCh := make(chan []User, 1)
postsCh := make(chan []Post, 1)
go func() { usersCh <- db.GetUsers() }()
go func() { postsCh <- db.GetPosts() }()
users := <-usersCh // blocks until ready
posts := <-postsChRust (tokio)
Tokio's select! and join! macros have a similar shape to Hew's expressions. They are library macros rather than language constructs, they need the Tokio runtime, and select! branches that touch shared state must satisfy the borrow checker.
// Race: tokio::select! macro
let winner = tokio::select! {
x = primary.fetch(key) => x.unwrap_or(-1),
y = fallback.fetch(key) => y.unwrap_or(-1),
_ = tokio::time::sleep(
Duration::from_millis(100)
) => Err("timeout"),
};
// Join: tokio::join! macro
let (users, posts) = tokio::join!(
db.get_users(),
db.get_posts(),
);What this shows
Developer experience
Hew's select and await fork (...) are expressions, so the result binds directly with let. Go's select is a statement, so results land in variables declared outside the block. Tokio's macros are also expressions, provided by a runtime dependency.
Timeouts and cancellation
In Hew, the timeout is visible in the select expression as an after clause. Older Go timer patterns had resource-lifetime pitfalls in tight loops, and any losing goroutine still needs explicit cancellation via a context if continuing work would be harmful.
Trade-offs
Go's channels are a general communication primitive between goroutines, and Tokio's macros accept any future. Hew's select covers tasks, actor calls, stream receives, ordinary calls and after timers, and await fork (...) joins any set of calls, both without channel setup. Losing arms are cleaned up by kind: a losing actor call is withdrawn and a losing ordinary call or timer is cancelled, while a losing task keeps running under its scope.