Promises, start and await
await in Raku 6.d is non-blocking for the thread pool: when called from a pool thread, the continuation is scheduled, freeing the thread.
# start runs a block on the thread pool and returns a Promise
my $p = start { sleep 0.5; 42 };
say $p.status; # Planned
say await $p; # 42 (blocks until kept)
say $p.status; # Kept
# Run many tasks in parallel
my @promises = (1..5).map: -> $n { start { sleep 0.1 * $n; $n ** 2 } };
say await @promises; # (1 4 9 16 25)
# Combinators
my $all = Promise.allof(@promises); # kept when all are done (kept or broken)
my $any = Promise.anyof(Promise.in(2), start { sleep 1; 'fast' }); # first to finish
await $any;
my $timeout = Promise.in(1); # kept after 1 second
my $at = Promise.at(now + 2);
# then: chain a continuation
my $chained = start { 10 }.then({ .result * 2 });
say await $chained; # 20
# Error handling: a broken Promise rethrows on await
my $bad = start { die "worker failed" };
try { await $bad; CATCH { default { say "Caught: {.message}" } } }
say $bad.status; # Broken
say $bad.cause.message; # worker failed
# Manual Promises with a vow
my $manual = Promise.new;
my $vow = $manual.vow;
start { sleep 0.1; $vow.keep('done manually') };
say await $manual;
# Timeouts
my
…