How to break a promise as part of a promise?

How to break a promise as part of a promise? Perl6 docs do not provide an easy way. For example:

my $x = start { loop { # loop forever until "quit" is seen my $y = prompt("Say something: "); if $y ~~ / quit / { # I want to exit the promise from here; # "break" and "this.break" are not defined; # "return" does not break the promise; # I do NOT want an error exception when exiting a promise; # I want to return a value as the result of this promise; } else { say $y; } } } 

I do not want to be in the cycle of promises forever. break() and this.break() not recognized, and return does not break the promise.

+7
promise break perl6
source share
1 answer

Use the last keyword to exit the loop.

The stored value of the start block is the value returned by its last statement.

So:

 my $x = start { loop { # loop forever until "quit" is seen my $y = prompt("Say something: "); if $y ~~ / quit / { last } else { say $y; } } 42 # <-- The promise will be kept with value `42` } 
+7
source share

All Articles