The then method, when called on an already existing promise, creates another promise, whose code will be called after the “parent” promise is either kept or broken.
my $p = Promise.in(2); my $t = $p.then({say "OK"}); # Prints this in two seconds say "promised"; # Prints immediately sleep 3; say "done";
The code above produces the following output:
promised OK done
In another example, the promise is broken.
Promise.start({ # A new promise say 1 / 0 # generates an exception # (the result of the division is used in say). }).then({ # The code executed after the broken line. say "oops" }).result # This is required so that we wait until # the result is known.
The only output here is the following:
oops