Course of Raku / Functional, concurrent, reactive, and web programming / Concurrent programming / Promises
start and await
The start keyword runs a block in the background and
immediately returns a Promise:
my $p = start { 2 + 2 };The work happens on Raku’s thread pool while your program carries on.
To get the result, use await, which waits for the promise
to finish and gives you its value:
my $p = start { 2 + 2 };
say await $p; # 4await blocks just long enough for the promise to
complete, then returns whatever the block produced — here,
4. If the work is already done by the time you await, you
get the result at once.
This pair — start to launch, await to
collect — is the core of promise-based concurrency. You can launch
several pieces of work, let them run at the same time, and await their
results when you are ready, which is exactly what the next topic builds
on.
Practice
Complete the quiz that covers the contents of this topic.
Course navigation
← Promises | Quiz — Promises →
💪 Or jump directly to
the exercises in this section.