note
moritz
<p>Concurrency is not my big strength, so this will likely turn out to be a bit more clunky than necessary.</p>
<p>One possible trick is to use the [https://docs.perl6.org/type/Promise#method_anyof|Promise.anyof] combinator to get the first of the timeout or the actual receiving action:</p>
<code>
my $c = channel("Message");
say "Listening";
loop {
my $timeout = Promise.in(1.75);
my $result = start { $c.receive }
my $combined = Promise.anyof( $result , $timeout);
await $combined.then({
if $result {
say $result.result;
}
else {
say 'timeout';
}
})
}
</code>
<p>This puts a timeout on every individual .receive, but finishes early if a value is available earlier. It's also not a busy loop, so doesn't use much CPU.</p>
<p>I'm sure there are much more elegant solutions out there if you use a supply instead of a channel to generate the values; maybe some of the promise combinators like [https://docs.perl6.org/type/Supply#method_zip-latest|zip-latest] can be used then.</p>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-616540">
[http://perl6.org/|Perl 6 - the future is here, just unevenly distributed]
</div></div>
1168103
1168103