Course of Raku / Functional, concurrent, reactive, and web programming / Concurrent programming / Promises / Exercises / Await many

Solution: Await many

Here is a possible solution to the task.

Code

my @words = <apple pear plum>;
my @jobs = @words.map(-> $w { start { $w.uc } });
say await @jobs;

🦋 You can find the source code in the file await-many.raku.

Output

(APPLE PEAR PLUM)

Comments

  1. @words.map(-> $w { start { $w.uc } }) turns each word into its own promise, so all three upper-case concurrently. The pointy block names the word $w, so every promise captures the right one.

  2. await @jobs waits for the whole list and hands back the results in their original order, giving (APPLE PEAR PLUM).

Course navigation

Await many   |   Kept or broken