Skip to content

Commit a75537b

Browse files
committed
largely done with Promises
1 parent a91940a commit a75537b

File tree

1 file changed

+65
-45
lines changed

1 file changed

+65
-45
lines changed

lib/Language/concurrency.pod

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ then it will return the result (that is the value passed to C<keep>, )
158158
or it will throw an exception based on the value passed to C<break>. The
159159
latter behaviour is illustrated with:
160160
161-
my $promise1 = Promise.new();
162-
my $promise2 = $promise1.then(-> $v { say "Handled but : "; say $v.result});
163-
$promise1.break("First Result");
164-
try $promise2.result;
165-
say $promise2.cause;
161+
my $promise1 = Promise.new();
162+
my $promise2 = $promise1.then(-> $v { say "Handled but : "; say $v.result});
163+
$promise1.break("First Result");
164+
try $promise2.result;
165+
say $promise2.cause;
166166
167167
Here the C<break> will cause the code block of the C<then> to throw an
168168
exception when it calls the C<result> method on the original promise
@@ -174,9 +174,9 @@ L<X::Promise::CauseOnlyValidOnBroken> exception.
174174
175175
A L<Promise> can also be scheduled to be automatically kept at a future time:
176176
177-
my $promise1 = Promise.in(5);
178-
my $promise2 = $promise1.then(-> $v { say $v.status; 'Second Result' });
179-
say $promise2.result;
177+
my $promise1 = Promise.in(5);
178+
my $promise2 = $promise1.then(-> $v { say $v.status; 'Second Result' });
179+
say $promise2.result;
180180
181181
The method C<in> creates a new promise and schedules a new task to call
182182
C<keep> on it no earlier than the supplied number of seconds, returning
@@ -188,61 +188,81 @@ be executed as soon as possible and obtain a promise that will be kept
188188
(or broken) when the code completes (or throws an exception,) this is
189189
performed by the method C<start> :
190190
191-
my $promise = Promise.start({ my $i = 0; for 1 .. 10 { $i += $_ }; $i});
192-
say $promise.result;
191+
my $promise = Promise.start({ my $i = 0; for 1 .. 10 { $i += $_ }; $i});
192+
say $promise.result;
193193
194194
Here the C<result> of the promise returned is the value returned from
195195
the code. Similarly if the code fails (and the promise is thus broken,)
196196
then C<cause> will be the L<Exception> object that was thrown:
197197
198-
my $promise = Promise.start({ die "Broken Promise" });
199-
try $promise.result;
200-
say $promise.cause;
198+
my $promise = Promise.start({ die "Broken Promise" });
199+
try $promise.result;
200+
say $promise.cause;
201201
202202
This is considered to be such a commonly required pattern that it is
203203
provided as subroutines:
204204
205-
my $result = await start {
206-
my $i = 0;
207-
for 1 .. 10 {
208-
$i += $_
209-
}
210-
$i
205+
my $result = await start {
206+
my $i = 0;
207+
for 1 .. 10 {
208+
$i += $_
209+
}
210+
$i
211211
}
212-
say $result;
212+
say $result;
213213
214214
C<await> is almost equivalent to calling C<result> on the promise object
215215
returned by C<start> but it will also take a list of promises and return
216216
the result of each:
217217
218-
my $result = await start({
219-
my $i = 0;
220-
for 1 .. 10 {
221-
$i += $_
222-
}
223-
$i
224-
}),
225-
start({
226-
my $i = 0;
227-
for 1 .. 10 {
228-
$i -= $_
229-
}
230-
$i
231-
});
232-
say $result;
233-
234-
235-
236-
237-
238-
218+
my $result = await start({
219+
my $i = 0;
220+
for 1 .. 10 {
221+
$i += $_
222+
}
223+
$i
224+
}),
225+
start({
226+
my $i = 0;
227+
for 1 .. 10 {
228+
$i -= $_
229+
}
230+
$i
231+
});
232+
say $result;
233+
234+
In addition to C<await> two methods are provided that will combine a set pf
235+
L<Promise> objects into a new promise: C<allof> which returns a promise that
236+
is kept when all the original promises are kept or broken when one of them
237+
is broken:
238+
239+
my $promise = Promise.allof(
240+
Promise.in(2),
241+
Promise.in(3)
242+
);
243+
244+
await $promise;
245+
say "All done"; # Should be no less than three seconds later
246+
247+
And C<anyof> which returns a new promise that will be kept when any of the
248+
original promises is kept or broken when any of them is broken:
249+
250+
my $promise = Promise.anyof(
251+
Promise.in(3),
252+
Promise.in(8600)
253+
);
254+
255+
await $promise;
256+
say "All done"; # Should be about 3 seconds later
257+
258+
unlike C<await> however the results of the original kept promises are not
259+
available without referring to the original, so these are more useful
260+
when the completion or otherwise of the tasks is more important to the
261+
consumer than the actual results, or the results have been collected by
262+
other means.
239263
240264
=begin comment
241265
242-
=head3 start
243-
244-
=head3 await
245-
246266
=head2 supplies
247267
248268
=head3 tap

0 commit comments

Comments
 (0)