How do I exit a promise from within a promise? The perl6 docs do not provide an easy method. 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 promise loop forever. break()
and this.break()
are not recognized, and return
does not break the promise.
Use the last
keyword to quit the loop.
The kept value of a 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`
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With