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

Solution: Await a supply

Here is a possible solution to the task.

Code

my $sensor = Supply.from-list(18, 21, 19, 23);

my $last = await $sensor;
say "final reading: $last";

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

Output

final reading: 23

Comments

  1. Awaiting a supply blocks until the stream has emitted everything it has — the same “wait until complete” that await gives for a promise — and its value is the last thing the supply emitted, 23.

  2. This suits a stream where only the end state matters, like the latest sensor reading. If you need every value, that is a job for .tap or a react block, as in the previous sections.

Course navigation

Await a supply   |   Await a failure