Select readiness and cancel
select returns the index of the first future to finish. cancel requests cooperative cancellation, and is_cancelled lets long-running work decide when to stop.
Cancellation is a request, so a task can finish its cleanup before it settles.
async fn later(value: int, delay: int) -> int {
await sleep(delay)
value
}
async fn main() {
let slow = spawn(later(10, 20))
let fast = spawn(later(20, 5))
println(await select([slow, fast]))
cancel(slow)
}
Examples use the moon toolchain version shown beside each result.
Output & diagnostics
Expected: 1
Ctrl/⌘+Enter runs the current lesson.