@@ -94,18 +94,18 @@ A L<Promise> can also be scheduled to be automatically kept at a future time:
94
94
my $promise2 = $promise1.then(-> $v { say $v.status; 'Second Result' });
95
95
say $promise2.result;
96
96
97
- The method C < in > creates a new promise and schedules a new task to call
97
+ The L < method in|/type/Promise#method_in > creates a new promise and schedules a new task to call
98
98
C < keep > on it no earlier than the supplied number of seconds, returning
99
- the new L < Promise > object. This is almost directly equivalent to calling
100
- C < Scheduler.cue > with a code block that will C < keep > the returned promise.
99
+ the new L < Promise > object.
101
100
102
- Possibly the most frequent requirement would be to schedule some code to
103
- be executed as soon as possible and obtain a promise that will be kept
104
- (or broken) when the code completes (or throws an exception,) this is
105
- performed by the method C < start > :
101
+ A very frequent use of promises is to run a piece of code, and keep the
102
+ promise once it returns successfully, or break it when the code dies.
103
+ The L < start method|/type/Promise#method_start > provides a shortcut for that:
106
104
107
- my $promise = Promise.start({ my $i = 0; for 1 .. 10 { $i += $_ }; $i});
108
- say $promise.result;
105
+ my $promise = Promise.start(
106
+ { my $i = 0; for 1 .. 10 { $i += $_ }; $i}
107
+ );
108
+ say $promise.result; # 55
109
109
110
110
Here the C < result > of the promise returned is the value returned from
111
111
the code. Similarly if the code fails (and the promise is thus broken,)
@@ -116,62 +116,64 @@ then C<cause> will be the L<Exception> object that was thrown:
116
116
say $promise.cause;
117
117
118
118
This is considered to be such a commonly required pattern that it is
119
- provided as subroutines:
120
-
121
- my $result = await start {
122
- my $i = 0;
123
- for 1 .. 10 {
124
- $i += $_
125
- }
126
- $i
127
- }
119
+ also provided as subroutines:
120
+
121
+ my $promise = start {
122
+ my $i = 0;
123
+ for 1 .. 10 {
124
+ $i += $_
125
+ }
126
+ $i
127
+ }
128
+ my $result = await $promise;
128
129
say $result;
129
130
130
131
C < await > is almost equivalent to calling C < result > on the promise object
131
132
returned by C < start > but it will also take a list of promises and return
132
133
the result of each:
133
134
134
- my $result = await start({
135
- my $i = 0;
136
- for 1 .. 10 {
137
- $i += $_
138
- }
139
- $i
140
- }),
141
- start({
142
- my $i = 0;
143
- for 1 .. 10 {
144
- $i -= $_
145
- }
146
- $i
147
- });
148
- say $result;
149
-
150
- In addition to C < await > two methods are provided that will combine a set pf
151
- L < Promise > objects into a new promise: C < allof > which returns a promise that
135
+ my $p1 = start {
136
+ my $i = 0;
137
+ for 1 .. 10 {
138
+ $i += $_
139
+ }
140
+ $i
141
+ };
142
+ my $p2 = start {
143
+ my $i = 0;
144
+ for 1 .. 10 {
145
+ $i -= $_
146
+ }
147
+ $i
148
+ };
149
+ my @result = await $p1, $p2;
150
+ say @result; # 55 -55
151
+
152
+ In addition to C < await > , two class methods combine several
153
+ L < Promise > objects into a new promise: C < allof > returns a promise that
152
154
is kept when all the original promises are kept or broken when one of them
153
155
is broken:
154
156
155
157
my $promise = Promise.allof(
156
- Promise.in(2),
157
- Promise.in(3)
158
- );
158
+ Promise.in(2),
159
+ Promise.in(3)
160
+ );
159
161
160
162
await $promise;
161
- say "All done"; # Should be no less than three seconds later
163
+ say "All done"; # Should be not much more than three seconds later
162
164
163
- And C < anyof > which returns a new promise that will be kept when any of the
165
+ And C < anyof > returns a new promise that will be kept when any of the
164
166
original promises is kept or broken when any of them is broken:
165
167
166
168
my $promise = Promise.anyof(
167
- Promise.in(3),
168
- Promise.in(8600)
169
- );
169
+ Promise.in(3),
170
+ Promise.in(8600)
171
+ );
170
172
171
173
await $promise;
172
174
say "All done"; # Should be about 3 seconds later
173
175
174
- unlike C < await > however the results of the original kept promises are not
176
+ Unlike C < await > however the results of the original kept promises are not
175
177
available without referring to the original, so these are more useful
176
178
when the completion or otherwise of the tasks is more important to the
177
179
consumer than the actual results, or the results have been collected by
@@ -185,12 +187,12 @@ consumed by one or more consumers simultaneously in a manner similar to
185
187
"events" in other programming languages and can be seem as enabling
186
188
"Event Driven" or reactive designs.
187
189
188
- L < Supply > is a role that can be composed into your own types, but will
190
+ L < Supply > is a role that can be composed into your own types, but will
189
191
frequently be used as a "type punned" class to create standalone objects.
190
192
191
193
At its simplest a L < Supply > is a message stream that can have multiple
192
194
subscribers created with the method C < tap > on to which data items can be
193
- placed with C < emit > :
195
+ placed with C < emit > :
194
196
195
197
my $supply = Supply.new();
196
198
@@ -211,25 +213,25 @@ interested in the events:
211
213
$supply.emit("Won't trigger the tap");
212
214
213
215
Calling C < close > on the L < Tap > object (or calling C < close > on the supply
214
- object with the tap object as an argument,) will trigger the callback
216
+ object with the tap object as an argument,) triggers the callback
215
217
specified by C < closing > if present and stop further events being sent to
216
218
the tap callback but otherwise leave the supply intact to receive further
217
- events. Calling C < done > on the supply object will similarly call the
218
- C < done > callback that may be specified for any taps but will not prevent any
219
+ events. Calling C < done > on the supply object similarly calls the
220
+ C < done > callback that may be specified for any taps, but does not prevent any
219
221
further events being emitted to the stream, or taps receiving them.
220
222
221
- The method C < interval > will return a new supply which will automatically
222
- emit a new event at the specified interval, the data that is emitted will
223
- be an integer starting at 0 that will be incremented for each event. The
224
- following code will output 0 .. 5 :
223
+ The method C < interval > returns a new supply which peridically
224
+ emits a new event at the specified interval. The data that is emitted
225
+ is an integer starting at 0 that is incremented for each event. The
226
+ following code outputs 0 .. 5 :
225
227
226
228
my $supply = Supply.interval(2);
227
229
$supply.tap(-> $v { say $v });
228
230
sleep 10;
229
231
230
- A second argument can be supplied which will specify a delay in seconds
232
+ A second argument can be supplied which specifies a delay in seconds
231
233
before the first event is fired. Each tap of a supply created by
232
- C < interval > will have its own sequence starting from 0, as illustrated
234
+ C < interval > has its own sequence starting from 0, as illustrated
233
235
by the following:
234
236
235
237
my $supply = Supply.interval(2);
@@ -241,8 +243,8 @@ by the following:
241
243
An existing supply object can be filtered or transformed, using the methods
242
244
C < grep > and C < map > respectively, to create a new supply in a manner like
243
245
the similarly named list methods: C < grep > returns a supply such that only
244
- those events emitted on the source stream for which the expression provided
245
- to the C < grep > will be enitted :
246
+ those events emitted on the source stream for which the C < grep > condition
247
+ is true is emitted on the second supply :
246
248
247
249
my $supply = Supply.new;
248
250
$supply.tap(-> $v { say "Original : $v" });
@@ -254,9 +256,9 @@ to the C<grep> will be enitted:
254
256
$supply.emit($_);
255
257
}
256
258
257
- C < map > will return a new supply such that for each item emitted to the
259
+ C < map > returns a new supply such that for each item emitted to the
258
260
original supply a new item which is the result of the expression passed
259
- to the C < map > will be emitted:
261
+ to the C < map > is emitted:
260
262
261
263
my $supply = Supply.new;
262
264
$supply.tap(-> $v { say "Original : $v" });
0 commit comments