On Tue Aug 02 19:37:27 2016, liu...@360.cn wrote:
> liukan@candor:~/Code$ cat p1.pl6
> use v6;
> 
> my $p = Promise.start: { sleep 1; 42 };
> $p.then: { say .result }; $p.result;
> my $s = $p.status;
> 
> #say $s;
> exit 0;
> 
> liukan@candor:~/Code$ perl6 p1.pl6
> 42
> ## this is OK
> 
It's not really OK, and won't work reliably (I did 20 runs and it gave no 
output in 3 of those runs). $p.then(...) returns a Promise that will complete 
when the follow-up work is complete. However, you never wait for *that* second 
Promise. Waiting for $p.result is not good enough, because it doesn't say 
anything about whether the work in the `then` has completed.

The only reason the exit appears to help is because it does a little more work, 
extending the runtime of the program overall just long enough for the output to 
happen a little more of the time.

A correct program would be:

my $p2 = $p.then: { say .result }; $p2.result;

Or simply:

await $p.then: { say .result };

Hope this helps,

Jonathan

Reply via email to