Timeout Operation

Author: stmuk

You want to timeout an operation (possibly long running)

Source code: 16-01-operation-timeout.p6

use v6;

constant timeout = 3;

my $done = start {
    say "starting to sleep...";
    sleep 10; # long running operation here
}

await Promise.anyof($done, Promise.in(timeout));

if $done {
    say "operation completed";
}
else {
    warn "timed out after {timeout} sec(s)";
}