Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit a promise from within a promise?

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.

like image 740
lisprogtor Avatar asked Aug 21 '17 05:08

lisprogtor


1 Answers

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`
}
like image 89
raiph Avatar answered Oct 04 '22 07:10

raiph