Course of Raku / Functional, concurrent, reactive, and web programming / Reactive programming / await / Exercises / Await a promise
Solution: Await a promise
Here is a possible solution to the task.
Code
my @jobs =
(start { sleep 0.3; 'slow' }),
(start { sleep 0.1; 'quick' }),
(start { sleep 0.2; 'medium' });
say await @jobs;🦋 You can find the source code in the file await-a-promise.raku.
Output
(slow quick medium)Comments
All three jobs start at once and sleep concurrently, so the whole program takes about
0.3seconds — the duration of the slowest job, not the sum of all three.Although
quickfinishes first andslowlast, the output is(slow quick medium):awaitreturns each result at the same position as its promise in@jobs. Completion order affects only whenawaitreturns, never the arrangement of the results.