📘 The start keyword in Perl 6 promises

The start method creates a promise containing a block of code. There is an alternative way to create a promise by calling Promise.start via the start keyword. my $p = start {     42 } (Note that in Perl 6, a semicolon is assumed after a closing brace at the end of a line.) The … Continue reading “📘 The start keyword in Perl 6 promises”

📘 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 … Continue reading “📘 The then method in Perl 6 promises”

📘 The anyof and allof methods in Perl 6 promises

Another pair of factory methods, Promise.anyof and Promise.allof, creates new promises, which will be only kept when at least one of the promises (in the case of anyof) is kept or, in the case of allof, all of the promises listed at the moment of creation are kept. One of the useful examples found in … Continue reading “📘 The anyof and allof methods in Perl 6 promises”

📘 An example of using promises: Sleep sort in Perl 6

Finally, a funny example of how promises can be used for implementing the sleep sort algorithm. In sleep sort, every integer number, consumed from the input, creates a delay proportional to its value. As the sleep is over, the number is printed out. Promises are exactly the things that will execute the code and tell … Continue reading “📘 An example of using promises: Sleep sort in Perl 6”