Course of Raku / Functional, concurrent, reactive, and web programming / Reactive programming / await / Exercises / Await a failure

Solution: Await a failure

Here is a possible solution to the task.

Code

my $p = start { die 'boom' };

try {
    await $p;
    CATCH {
        default { say "caught: {.message}" }
    }
}

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

Output

caught: boom

Comments

  1. The promise’s block throws, so the promise is broken. The exception is not lost — it is held until someone awaits the promise.

  2. await $p rethrows it right there, where the CATCH phaser handles it like any ordinary exception. This is how errors in background work surface where you wait for the result.

Course navigation

Await a failure   |   Web programming