1Mojo::IOLoop::Delay(3)User Contributed Perl DocumentationMojo::IOLoop::Delay(3)
2
3
4
6 Mojo::IOLoop::Delay - Promises/A+ and flow-control helpers
7
9 use Mojo::IOLoop::Delay;
10
11 # Synchronize multiple non-blocking operations
12 my $delay = Mojo::IOLoop::Delay->new;
13 $delay->steps(sub { say 'BOOM!' });
14 for my $i (1 .. 10) {
15 my $end = $delay->begin;
16 Mojo::IOLoop->timer($i => sub {
17 say 10 - $i;
18 $end->();
19 });
20 }
21 $delay->wait;
22
23 # Sequentialize multiple non-blocking operations
24 Mojo::IOLoop::Delay->new->steps(
25
26 # First step (simple timer)
27 sub {
28 my $delay = shift;
29 Mojo::IOLoop->timer(2 => $delay->begin);
30 say 'Second step in 2 seconds.';
31 },
32
33 # Second step (concurrent timers)
34 sub {
35 my ($delay, @args) = @_;
36 Mojo::IOLoop->timer(1 => $delay->begin);
37 Mojo::IOLoop->timer(3 => $delay->begin);
38 say 'Third step in 3 seconds.';
39 },
40
41 # Third step (the end)
42 sub {
43 my ($delay, @args) = @_;
44 say 'And done after 5 seconds total.';
45 }
46 )->wait;
47
49 Mojo::IOLoop::Delay adds flow-control helpers to Mojo::Promise, which
50 can help you avoid deep nested closures that often result from
51 continuation-passing style.
52
53 use Mojo::IOLoop;
54
55 # These deep nested closures are often referred to as "Callback Hell"
56 Mojo::IOLoop->timer(3 => sub {
57 my loop = shift;
58
59 say '3 seconds';
60 Mojo::IOLoop->timer(3 => sub {
61 my $loop = shift;
62
63 say '6 seconds';
64 Mojo::IOLoop->timer(3 => sub {
65 my $loop = shift;
66
67 say '9 seconds';
68 Mojo::IOLoop->stop;
69 });
70 });
71 });
72
73 Mojo::IOLoop->start;
74
75 The idea behind Mojo::IOLoop::Delay is to turn the nested closures
76 above into a flat series of closures. In the example below, the call to
77 "begin" creates a code reference that we can pass to "timer" in
78 Mojo::IOLoop as a callback, and that leads to the next closure in the
79 series when executed.
80
81 use Mojo::IOLoop;
82
83 # Instead of nested closures we now have a simple chain of steps
84 my $delay = Mojo::IOLoop->delay(
85 sub {
86 my $delay = shift;
87 Mojo::IOLoop->timer(3 => $delay->begin);
88 },
89 sub {
90 my $delay = shift;
91 say '3 seconds';
92 Mojo::IOLoop->timer(3 => $delay->begin);
93 },
94 sub {
95 my $delay = shift;
96 say '6 seconds';
97 Mojo::IOLoop->timer(3 => $delay->begin);
98 },
99 sub {
100 my $delay = shift;
101 say '9 seconds';
102 }
103 );
104 $delay->wait;
105
106 Another positive side effect of this pattern is that we do not need to
107 call "start" in Mojo::IOLoop and "stop" in Mojo::IOLoop manually,
108 because we know exactly when our chain of "steps" has reached the end.
109 So "wait" in Mojo::Promise can stop the event loop automatically if it
110 had to be started at all in the first place.
111
113 Mojo::IOLoop::Delay inherits all attributes from Mojo::Promise.
114
116 Mojo::IOLoop::Delay inherits all methods from Mojo::Promise and
117 implements the following new ones.
118
119 begin
120 my $cb = $delay->begin;
121 my $cb = $delay->begin($offset);
122 my $cb = $delay->begin($offset, $len);
123
124 Indicate an active event by incrementing the event counter, the
125 returned code reference can be used as a callback, and needs to be
126 executed when the event has completed to decrement the event counter
127 again. When all code references generated by this method have been
128 executed and the event counter has reached zero, "steps" will continue.
129
130 # Capture all arguments except for the first one (invocant)
131 my $delay = Mojo::IOLoop->delay(sub {
132 my ($delay, $err, $stream) = @_;
133 ...
134 });
135 Mojo::IOLoop->client({port => 3000} => $delay->begin);
136 $delay->wait;
137
138 Arguments passed to the returned code reference are spliced with the
139 given offset and length, defaulting to an offset of 1 with no default
140 length. The arguments are then combined in the same order "begin" was
141 called, and passed together to the next step.
142
143 # Capture all arguments
144 my $delay = Mojo::IOLoop->delay(sub {
145 my ($delay, $loop, $err, $stream) = @_;
146 ...
147 });
148 Mojo::IOLoop->client({port => 3000} => $delay->begin(0));
149 $delay->wait;
150
151 # Capture only the second argument
152 my $delay = Mojo::IOLoop->delay(sub {
153 my ($delay, $err) = @_;
154 ...
155 });
156 Mojo::IOLoop->client({port => 3000} => $delay->begin(1, 1));
157 $delay->wait;
158
159 # Capture and combine arguments
160 my $delay = Mojo::IOLoop->delay(sub {
161 my ($delay, $three_err, $three_stream, $four_err, $four_stream) = @_;
162 ...
163 });
164 Mojo::IOLoop->client({port => 3000} => $delay->begin);
165 Mojo::IOLoop->client({port => 4000} => $delay->begin);
166 $delay->wait;
167
168 pass
169 $delay = $delay->pass;
170 $delay = $delay->pass(@args);
171
172 Shortcut for passing values between "steps".
173
174 # Longer version
175 $delay->begin(0)->(@args);
176
177 steps
178 $delay = $delay->steps(sub {...}, sub {...});
179
180 Sequentialize multiple events, every time the event counter reaches
181 zero a callback will run, the first one automatically runs during the
182 next reactor tick unless it is delayed by incrementing the event
183 counter. This chain will continue until there are no remaining
184 callbacks, a callback does not increment the event counter or an
185 exception gets thrown in a callback. Finishing the chain will also
186 result in the promise being fulfilled, or if an exception got thrown it
187 will be rejected.
188
190 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
191
192
193
194perl v5.30.0 2019-07-26 Mojo::IOLoop::Delay(3)