@@ -256,16 +256,20 @@ Or C<break> it:
256
256
The current status of a C<Promise> is available through the C<status> method,
257
257
which returns an element from the C<PromiseStatus> enumeration.
258
258
259
- enum PromiseStatus (:Planned(0), :Kept(1), :Broken(2));
259
+ enum PromiseStatus (:Planned(0), :Kept(1), :Broken(2), :Redeemed(3) );
260
260
261
261
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.
265
264
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.
269
273
270
274
if $promise {
271
275
if $promise.status == Kept {
@@ -282,9 +286,10 @@ C<Promise> rather than having it thrown.
282
286
You can also simply use a switch:
283
287
284
288
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" }
288
293
}
289
294
290
295
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
352
357
viewpoint of the promiser, the various built-in C<Promise> factory methods
353
358
and combinators use C<Promise::Vow> objects to represent that internal
354
359
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
359
365
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:
362
368
363
369
method in(Promise:U: $seconds, :$scheduler = $*SCHEDULER) {
364
370
my $p = Promise.new(:$scheduler);
0 commit comments