📘 The then method in Perl 6 promises

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:


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:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s