Course of Raku / Functional, concurrent, reactive, and web programming / Concurrent programming / Promises / Exercises / Chain a promise

Solution: Chain a promise

Here is a possible solution to the task.

Code

my $p = start { 10 };
my $q = $p.then({ .result * 2 });
say await $q;

🦋 You can find the source code in the file promise-result.raku.

Output

20

Comments

  1. .then builds a new promise $q that runs once $p is done. Inside the block, .result is the value of the original promise, 10.

  2. The follow-up doubles it, so awaiting $q yields 20. Chaining with .then lets you build a pipeline where each step depends on the previous one’s result.

Course navigation

Chain a promise   |   Await many