ES6.pm

filter: Statementstmt 80.4% 78 / 97 Branchbran 81.8% 18 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 21 / 24 Podpod 12.5% 1 / 8 totaltotal 78.1% 118 / 151 CC 26 SCAR 7.7
CC
26
Cov
81%
__ANON__
4.0
__ANON__
4.0
__ANON__
3.0
CRAP 2.2
« ES6/EventLoopBase.pm ↑ Summary ES6/AnyEvent.pm »
1 package Promise::ES6;
2  
3 42 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 42 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 42 our $VERSION = '0.28';
7  
8 =encoding utf-8
9  
10 =head1 NAME
11  
12 Promise::ES6 - ES6-style promises in Perl
13  
14 =head1 SYNOPSIS
15  
16 use Promise::ES6;
17  
18 # OPTIONAL. And see below for other options.
19 Promise::ES6::use_event('IO::Async', $loop);
20  
21 my $promise = Promise::ES6->new( sub {
22 my ($resolve_cr, $reject_cr) = @_;
23  
24 # ..
25 } );
26  
27 my $promise2 = $promise->then( sub { .. }, sub { .. } );
28  
29 my $promise3 = $promise->catch( sub { .. } );
30  
31 my $promise4 = $promise->finally( sub { .. } );
32  
33 my $resolved = Promise::ES6->resolve(5);
34 my $rejected = Promise::ES6->reject('nono');
35  
36 my $all_promise = Promise::ES6->all( \@promises );
37  
38 my $race_promise = Promise::ES6->race( \@promises );
39  
40 my $allsettled_promise = Promise::ES6->allSettled( \@promises );
41  
42 =head1 DESCRIPTION
43  
44 =begin html
45  
46 <a href='https://coveralls.io/github/FGasper/p5-Promise-ES6?branch=master'><img src='https://coveralls.io/repos/github/FGasper/p5-Promise-ES6/badge.svg?branch=master' alt='Coverage Status' /></a>
47  
48 =end html
49  
50 This module provides a Perl implementation of L<promises|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises>, a useful pattern
51 for coordinating asynchronous tasks.
52  
53 Unlike most other promise implementations on CPAN, this module
54 mimics ECMAScript 6’s L<Promise|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise>
55 interface. As the SYNOPSIS above shows, you can thus use patterns from
56 JavaScript in Perl with only minimal changes needed to accommodate language
57 syntax.
58  
59 This is a rewrite of an earlier module, L<Promise::Tiny>. It fixes several
60 bugs and superfluous dependencies in the original.
61  
62 =head1 STATUS
63  
64 This module is in use in production and, backed by a pretty extensive
65 set of regression tests, may be considered stable.
66  
67 =head1 INTERFACE NOTES
68  
69 =over
70  
71 =item * Promise resolutions and rejections accept exactly one argument,
72 not a list.
73  
74 =item * Unhandled rejections are reported via C<warn()>. (See below
75 for details.)
76  
77 =item * Undefined or empty rejection values trigger a warning.
78 This provides the same value as Perl’s own warning on C<die(undef)>.
79  
80 =item * The L<Promises/A+ test suite|https://github.com/promises-aplus/promises-tests> avoids testing the case where an “executor”
81 function’s resolve callback itself receives another promise, e.g.:
82  
83 my $p = Promise::ES6->new( sub ($res) {
84 $res->( Promise::ES6->resolve(123) );
85 } );
86  
87 What will $p’s resolution value be? 123, or the promise that wraps it?
88  
89 This module favors conformity with the ES6 standard, which
90 L<indicates intent|https://www.ecma-international.org/ecma-262/6.0/#sec-promise-executor> that $p’s resolution value be 123.
91  
92 =back
93  
94 =head1 COMPATIBILITY
95  
96 This module considers any object that has a C<then()> method to be a promise.
97 Note that, in the case of L<Future>, this will yield a “false-positive”, as
98 Future is not compatible with promises.
99  
100 (See L<Promise::ES6::Future> for more tools to interact with L<Future>.)
101  
102 =head1 B<EXPERIMENTAL:> ASYNC/AWAIT SUPPORT
103  
104 This module implements L<Future::AsyncAwait::Awaitable>. This lets you do
105 nifty stuff like:
106  
107 use Future::AsyncAwait;
108  
109 async sub do_stuff {
110 my $foo = await fetch_number_p();
111  
112 # NB: The real return is a promise that provides this value:
113 return 1 + $foo;
114 }
115  
116 my $one_plus_number = await do_stuff();
117  
118 … which roughly equates to:
119  
120 sub do_stuff {
121 return fetch_number_p()->then( sub { 1 + $foo } );
122 }
123  
124 do_stuff->then( sub {
125 $one_plus_number = shift;
126 } );
127  
128 =head1 UNHANDLED REJECTIONS
129  
130 This module’s handling of unhandled rejections has changed over time.
131 The current behavior is: if any rejected promise is DESTROYed without first
132 having received a catch callback, a warning is thrown.
133  
134 =head1 SYNCHRONOUS VS. ASYNCHRONOUS OPERATION
135  
136 In JavaScript, the following …
137  
138 Promise.resolve().then( () => console.log(1) );
139 console.log(2);
140  
141 … will log C<2> then C<1> because JavaScript’s C<then()> defers execution
142 of its callbacks until between iterations through JavaScript’s event loop.
143  
144 Perl, of course, has no built-in event loop. This module accommodates that by
145 implementing B<synchronous> promises by default rather than asynchronous ones.
146 This means that all promise callbacks run I<immediately> rather than between
147 iterations of an event loop. As a result, this:
148  
149 Promise::ES6->resolve(0)->then( sub { print 1 } );
150 print 2;
151  
152 … will print C<12> instead of C<21>.
153  
154 One effect of this is that Promise::ES6, in its default configuration, is
155 agnostic regarding event loop interfaces: no special configuration is needed
156 for any specific event loop. In fact, you don’t even I<need> an event loop
157 at all, which might be useful for abstracting over whether a given
158 function works synchronously or asynchronously.
159  
160 The disadvantage of synchronous promises—besides not being I<quite> the same
161 promises that we expect from JS—is that recursive promises can exceed
162 call stack limits. For example, the following (admittedly contrived) code:
163  
164 my @nums = 1 .. 1000;
165  
166 sub _remove {
167 if (@nums) {
168 Promise::ES6->resolve(shift @nums)->then(\&_remove);
169 }
170 }
171  
172 _remove();
173  
174 … will eventually fail because it will reach Perl’s call stack size limit.
175  
176 That problem probably won’t affect most applications. The best way to
177 avoid it, though, is to use asynchronous promises, à la JavaScript.
178  
179 To do that, first choose one of the following event interfaces:
180  
181 =over
182  
183 =item * L<IO::Async>
184  
185 =item * L<AnyEvent>
186  
187 =item * L<Mojo::IOLoop> (part of L<Mojolicious>)
188  
189 =back
190  
191 Then, before you start creating promises, do this:
192  
193 Promise::ES6::use_event('AnyEvent');
194  
195 … or:
196  
197 Promise::ES6::use_event('Mojo::IOLoop');
198  
199 … or:
200  
201 Promise::ES6::use_event('IO::Async', $loop);
202  
203 That’s it! Promise::ES6 instances will now work asynchronously rather than
204 synchronously.
205  
206 Note that this changes Promise::ES6 I<globally>. In IO::Async’s case, it
207 won’t increase the passed-in L<IO::Async::Loop> instance’s reference count,
208 but if that loop object goes away, Promise::ES6 won’t work until you call
209 C<use_event()> again.
210  
211 B<IMPORTANT:> For the best long-term scalability and flexibility,
212 your code should work with either synchronous or asynchronous promises.
213  
214 =head1 CANCELLATION
215  
216 Promises have never provided a standardized solution for cancellation—i.e.,
217 aborting an in-process operation. If you need this functionality, then, you’ll
218 have to implement it yourself. Two ways of doing this are:
219  
220 =over
221  
222 =item * Subclass Promise::ES6 and provide cancellation logic in that
223 subclass. See L<DNS::Unbound::AsyncQuery>’s implementation for an
224 example of this.
225  
226 =item * Implement the cancellation on a request object that your
227 “promise-creator” also consumes. This is probably the more straightforward
228 approach but requires that there
229 be some object or ID besides the promise that uniquely identifies the action
230 to be canceled. See L<Net::Curl::Promiser> for an example of this approach.
231  
232 =back
233  
234 You’ll need to decide if it makes more sense for your application to leave
235 a canceled query in the “pending” state or to “settle” (i.e., resolve or
236 reject) it. All things being equal, I feel the first approach is the most
237 intuitive, while the latter ends up being “cleaner”.
238  
239 Of note: L<Future> implements native cancellation.
240  
241 =head1 MEMORY LEAKS
242  
243 It’s easy to create inadvertent memory leaks using promises in Perl.
244 Here are a few “pointers” (heh) to bear in mind:
245  
246 =over
247  
248 =item * Any Promise::ES6 instances that are created while
249 C<$Promise::ES6::DETECT_MEMORY_LEAKS> is set to a truthy value are
250 “leak-detect-enabled”, which means that if they survive until their original
251 process’s global destruction, a warning is triggered. You should normally
252 enable this flag in a development environment.
253  
254 =item * If your application needs recursive promises (e.g., to poll
255 iteratively for completion of a task), the C<current_sub> feature (i.e.,
256 C<__SUB__>) may help you avoid memory leaks. In Perl versions that don’t
257 support this feature (i.e., anything pre-5.16) you can imitate it thus:
258  
259 use constant _has_current_sub => eval "use feature 'current_sub'";
260  
261 use if _has_current_sub(), feature => 'current_sub';
262  
263 my $cb;
264 $cb = sub {
265 my $current_sub = do {
266 no strict 'subs';
267 _has_current_sub() ? __SUB__ : eval '$cb';
268 };
269 }
270  
271 Of course, it’s better if you can avoid doing that. :)
272  
273 =item * Garbage collection before Perl 5.18 seems to have been buggy.
274 If you work with such versions and end up chasing leaks,
275 try manually deleting as many references/closures as possible. See
276 F<t/race_success.t> for a notated example.
277  
278 You may also (counterintuitively, IMO) find that this:
279  
280 my ($resolve, $reject);
281  
282 my $promise = Promise::ES6->new( sub { ($resolve, $reject) = @_ } );
283  
284 # … etc.
285  
286 … works better than:
287  
288 my $promise = Promise::ES6->new( sub {
289 my ($resolve, $reject) = @_;
290  
291 # … etc.
292 } );
293  
294 =back
295  
296 =head1 SEE ALSO
297  
298 If you’re not sure of what promises are, there are several good
299 introductions to the topic. You might start with
300 L<this one|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises>.
301  
302 L<Promise::XS> is my refactor of L<AnyEvent::XSPromises>. It’s a lot like
303 this library but implemented mostly in XS for speed.
304  
305 L<Promises> is another pure-Perl Promise implementation.
306  
307 L<Future> fills a role similar to that of promises. Much of the IO::Async
308 ecosystem assumes (or strongly encourages) its use.
309  
310 CPAN contains a number of other modules that implement promises. I think
311 mine are the nicest :), but YMMV. Enjoy!
312  
313 =head1 LICENSE & COPYRIGHT
314  
315 Copyright 2019-2021 Gasper Software Consulting.
316  
317 This library is licensed under the same terms as Perl itself.
318  
319 =cut
320  
321 #----------------------------------------------------------------------
322  
323 42 our $DETECT_MEMORY_LEAKS;
324  
325 0 ▶sub __default_postpone { die 'NO EVENT' }
Subroutine __default_postpone: not called CC=1 SCAR=6.9
326 42 *_postpone = \&__default_postpone;
327  
328 42 our $_EVENT;
329  
330 0 ▶sub use_event {
Subroutine use_event: not called CC=1 SCAR=4.9
Pod undocumented
331 0 ▶ my ($name, @args) = @_;
Subroutine use_event: not called CC=1 SCAR=4.9
Pod undocumented
332  
333 0 my $modname = $name;
334 0 $modname =~ tr<:><>d;
335  
336 0 my @saved_errs = ($!, $@);
337  
338 0 require "Promise/ES6/Event/$modname.pm";
339  
340 0 ($!, $@) = @saved_errs;
341  
342 0 $_EVENT = $name;
343  
344 # We need to block redefinition and (for AnyEvent)
345 # prototype-mismatch warnings.
346 42 ▶ no warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
347 0 *_postpone = "Promise::ES6::Event::$modname"->can('get_postpone')->(@args);
348  
349 0 return;
350 }
351  
352 20 ▶sub catch { $_[0]->then( undef, $_[1] ) }
Subroutine catch: called CC=1 SCAR=0.0
Pod undocumented
353  
354 20 ▶sub resolve {
Subroutine resolve: called CC=1 SCAR=0.0
Pod documented
355 20 my ( $class, $value ) = @_;
356  
357 20 ▶ $class->new( sub { $_[0]->($value) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
358 }
359  
360 13 ▶sub reject {
Subroutine reject: called CC=1 SCAR=0.0
Pod undocumented
361 13 my ( $class, @reason ) = @_;
362  
363 13 ▶ $class->new( sub { $_[1]->(@reason) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
364 }
365  
366 12 ▶sub all {
Subroutine all: called CC=2 SCAR=6.9
Pod undocumented
367 12 my ( $class, $iterable ) = @_;
368 12 ▶ my @promises = map { UNIVERSAL::can( $_, 'then' ) ? $_ : $class->resolve($_) } @$iterable;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can($_, 'then') ? : 19 4 23
369  
370 12 my @values;
371  
372 return $class->new(
373 12 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
374 12 my ( $resolve, $reject ) = @_;
375 12 my $unresolved_size = scalar(@promises);
376  
377 12 my $settled;
378  
379 12 ▶ if ($unresolved_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unresolved_size) { } 10 2 12
380 10 my $p = 0;
381  
382 7 ▶ my $on_reject_cr = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
383  
384 # Needed because we might get multiple failures:
385 7 ▶ return if $settled;
Branch: 1 branch
BranchTrueFalseTotal
if $settled 2 5 7
386  
387 5 $settled = 1;
388 5 $reject->(@_);
389 10 };
390  
391 10 for my $promise (@promises) {
392 23 my $p = $p++;
393  
394 $promise->then(
395 15 ▶ $settled ? undef : sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
396 15 ▶ return if $settled;
Branch: 1 branch
BranchTrueFalseTotal
if $settled 0 15 15
397  
398 15 $values[$p] = $_[0];
399  
400 15 $unresolved_size--;
401 15 ▶ return if $unresolved_size > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $unresolved_size > 0 10 5 15
402  
403 5 $settled = 1;
404 5 $resolve->( \@values );
405 },
406 23 ▶ $on_reject_cr,
Branch: 1 branch
BranchTrueFalseTotal
$settled ? : 3 20 23
407 );
408 }
409 }
410 else {
411 2 $resolve->( [] );
412 }
413 }
414 12 );
415 }
416  
417 5 ▶sub race {
Subroutine race: called CC=3 SCAR=11.0
Pod undocumented
418 5 my ( $class, $iterable ) = @_;
419 5 ▶ my @promises = map { UNIVERSAL::can( $_, 'then' ) ? $_ : $class->resolve($_) } @$iterable;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can($_, 'then') ? : 10 0 10
420  
421 5 my ( $resolve, $reject );
422  
423 # Perl 5.16 and earlier leak memory when the callbacks are handled
424 # inside the closure here.
425 my $new = $class->new(
426 5 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
427 5 ( $resolve, $reject ) = @_;
428 }
429 5 );
430  
431 5 my $is_done;
432  
433 3 ▶ my $on_resolve_cr = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
434 3 ▶ return if $is_done;
Branch: 1 branch
BranchTrueFalseTotal
if $is_done 0 3 3
435 3 $is_done = 1;
436  
437 3 $resolve->( $_[0] );
438  
439 # Proactively eliminate references:
440 3 $resolve = $reject = undef;
441 5 };
442  
443 3 ▶ my $on_reject_cr = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
444 3 ▶ return if $is_done;
Branch: 1 branch
BranchTrueFalseTotal
if $is_done 1 2 3
445 2 $is_done = 1;
446  
447 2 $reject->( $_[0] );
448  
449 # Proactively eliminate references:
450 2 $resolve = $reject = undef;
451 5 };
452  
453 5 for my $promise (@promises) {
454 10 $promise->then( $on_resolve_cr, $on_reject_cr );
455 }
456  
457 5 return $new;
458 }
459  
460 4 ▶sub _aS_fulfilled {
Subroutine _aS_fulfilled: called CC=1 SCAR=0.0
461 4 return { status => 'fulfilled', value => $_[0] };
462 }
463  
464 1 ▶sub _aS_rejected {
Subroutine _aS_rejected: called CC=1 SCAR=0.0
465 1 return { status => 'rejected', reason => $_[0] };
466 }
467  
468 5 ▶sub _aS_map {
Subroutine _aS_map: called CC=1 SCAR=0.0
469 5 return $_->then( \&_aS_fulfilled, \&_aS_rejected );
470 }
471  
472 3 ▶sub allSettled {
Subroutine allSettled: called CC=2 SCAR=6.9
Pod undocumented
473 3 my ( $class, $iterable ) = @_;
474  
475 3 ▶ my @promises = map { UNIVERSAL::can( $_, 'then' ) ? $_ : $class->resolve($_) } @$iterable;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can($_, 'then') ? : 4 1 5
476  
477 3 @promises = map( _aS_map, @promises );
478  
479 3 return $class->all( \@promises );
480 }
481  
482 #----------------------------------------------------------------------
483  
484 42 my $loaded_backend;
485  
486 42 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
487 # Put this block at the end so that the backend module
488 # can override any of the above.
489  
490 42 ▶ return if $loaded_backend;
Branch: 1 branch
BranchTrueFalseTotal
if $loaded_backend 0 42 42
491  
492 42 $loaded_backend = 1;
493  
494 # These don’t exist yet but will:
495 42 if (0 && !$ENV{'PROMISE_ES6_PP'} && eval { require Promise::ES6::XS }) {
496 require Promise::ES6::Backend::XS;
497 }
498  
499 # Fall back to pure Perl:
500 else {
501 42 require Promise::ES6::Backend::PP;
502 }
503 }
504  
505 42 1;
« ES6/EventLoopBase.pm ↑ Summary ES6/AnyEvent.pm »