Skip to content

Commit f136000

Browse files
committed
Add "Redeemed" status and .redeem to Promise
This would be the equivalent of Channel.close.
1 parent 9e4c566 commit f136000

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

S17-concurrency.pod

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,20 @@ Or C<break> it:
256256
The current status of a C<Promise> is available through the C<status> method,
257257
which returns an element from the C<PromiseStatus> enumeration.
258258

259-
enum PromiseStatus (:Planned(0), :Kept(1), :Broken(2));
259+
enum PromiseStatus (:Planned(0), :Kept(1), :Broken(2), :Redeemed(3));
260260

261261
The result itself can be obtained by calling C<result>. If the C<Promise> was
262-
already kept, the result is immediately returned. If the C<Promise> was broken
263-
then the exception that it was broken with is thrown. If the C<Promise> is not
264-
yet kept or broken, then the caller will block until this happens.
262+
already kept, the result is immediately returned. If the C<Promise> was
263+
broken then the exception that it was broken with is thrown.
265264

266-
A C<Promise> will boolify to whether the C<Promise> is already kept or broken.
267-
There is also an C<excuse> method for extracting the exception from a C<Broken>
268-
C<Promise> rather than having it thrown.
265+
By redeeming a C<Promise>, you are basically marking it as "closed". So, if
266+
a C<Promise> has been redeemed, then C<Nil> will be returned immediately.
267+
If the C<Promise> is not yet kept, broken or redeemed, then the caller will
268+
block until any of these status changes happen.
269+
270+
A C<Promise> will boolify to whether the C<Promise> is already kept, broken
271+
or redeemed. There is also an C<excuse> method for extracting the exception
272+
from a C<Broken> C<Promise> rather than having it thrown.
269273

270274
if $promise {
271275
if $promise.status == Kept {
@@ -282,9 +286,10 @@ C<Promise> rather than having it thrown.
282286
You can also simply use a switch:
283287

284288
given $promise.status {
285-
when Planned { say "Still working!" }
286-
when Kept { say "Kept, result = ", $promise.result }
287-
when Broken { say "Broken because ", $promise.excuse }
289+
when Planned { say "Still working!" }
290+
when Kept { say "Kept, result = ", $promise.result }
291+
when Broken { say "Broken because ", $promise.excuse }
292+
when Redeemed { say "Water under the bridge" }
288293
}
289294

290295
There are various convenient "factory" methods on C<Promise>. The most common
@@ -352,13 +357,14 @@ a promise is user-facing. To instead represent the promise from the
352357
viewpoint of the promiser, the various built-in C<Promise> factory methods
353358
and combinators use C<Promise::Vow> objects to represent that internal
354359
resolve to fulfill the promise. ("I have vowed to keep my promise
355-
to you.") The C<vow> method on a C<Promise> returns an object with C<keep>
356-
and C<break> methods. It can only be called once during a C<Promise> object's
357-
lifetime. Since C<keep> and C<break> on the C<Promise> itself just delegate
358-
to C<self.vow.keep(...)> or C<self.vow.break(...)>, obtaining the vow
360+
to you.") The C<vow> method on a C<Promise> returns an object with C<keep>,
361+
C<break> and C<redeem> methods. It can only be called once during a
362+
C<Promise> object's lifetime. Since C<keep>, C<break> and C<redeem> on the
363+
C<Promise> itself just delegate to C<self.vow.keep(...)>,
364+
C<self.vow.break(...)> or C<self.vow.redeem(...)>, obtaining the vow
359365
before letting the C<Promise> escape to the outside world is a way to take
360-
ownership of the right to keep or break it. For example, here is how the
361-
C<Promise.in> factory is implemented:
366+
ownership of the right to keep, break or redeem it. For example, here is how
367+
the C<Promise.in> factory is implemented:
362368

363369
method in(Promise:U: $seconds, :$scheduler = $*SCHEDULER) {
364370
my $p = Promise.new(:$scheduler);

0 commit comments

Comments
 (0)