Course of Raku / Functional, concurrent, reactive, and web programming / Reactive programming / await
Awaiting a promise
await takes a promise, waits for it to complete, and
returns its result:
my $p = start { 7 * 6 };
say await $p; # 42The program pauses at await only as long as needed, then
continues with the value the promise produced.
Given several promises, await waits for
all of them and returns their results in order:
my @jobs = (start { 5 }), (start { 10 }), (start { 15 });
say await @jobs; # (5 10 15)
say [+] await @jobs; # 30The jobs run concurrently, but await @jobs does not
return until every one is done, so the results are complete and in
order.
If an awaited promise was broken — its block threw
an exception — await rethrows that exception at the point
of the await. This means errors in background work surface
where you wait for it, so you can handle them with the usual
try / CATCH from the part on exceptions.
Awaiting is therefore not just about getting a value; it is the moment
where concurrent work rejoins the main flow, results and errors
alike.
Practice
Complete the quiz that covers the contents of this topic.
Course navigation
← await | Quiz — await →
💪 Or jump directly to
the exercises in this section.