@@ -158,11 +158,11 @@ then it will return the result (that is the value passed to C<keep>, )
158
158
or it will throw an exception based on the value passed to C < break > . The
159
159
latter behaviour is illustrated with:
160
160
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;
166
166
167
167
Here the C < break > will cause the code block of the C < then > to throw an
168
168
exception when it calls the C < result > method on the original promise
@@ -174,9 +174,9 @@ L<X::Promise::CauseOnlyValidOnBroken> exception.
174
174
175
175
A L < Promise > can also be scheduled to be automatically kept at a future time:
176
176
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;
180
180
181
181
The method C < in > creates a new promise and schedules a new task to call
182
182
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
188
188
(or broken) when the code completes (or throws an exception,) this is
189
189
performed by the method C < start > :
190
190
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;
193
193
194
194
Here the C < result > of the promise returned is the value returned from
195
195
the code. Similarly if the code fails (and the promise is thus broken,)
196
196
then C < cause > will be the L < Exception > object that was thrown:
197
197
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;
201
201
202
202
This is considered to be such a commonly required pattern that it is
203
203
provided as subroutines:
204
204
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
211
211
}
212
- say $result;
212
+ say $result;
213
213
214
214
C < await > is almost equivalent to calling C < result > on the promise object
215
215
returned by C < start > but it will also take a list of promises and return
216
216
the result of each:
217
217
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.
239
263
240
264
= begin comment
241
265
242
- = head3 start
243
-
244
- = head3 await
245
-
246
266
= head2 supplies
247
267
248
268
= head3 tap
0 commit comments