File Coverage

blib/lib/Promise/XS/Promise.pm
Criterion Covered Total %
statement 22 30 73.3
branch 3 6 50.0
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 30 43 69.7


line stmt bran cond sub pod time code
1             package Promise::XS::Promise;
2              
3 26     26   169 use strict;
  26         46  
  26         974  
4 26     26   123 use warnings;
  26         43  
  26         10503  
5              
6             =encoding utf-8
7              
8             =head1 NAME
9              
10             Promise::XS::Promise - promise object
11              
12             =head1 SYNOPSIS
13              
14             See L.
15              
16             =head1 DESCRIPTION
17              
18             This is L’s actual promise object class. It implements
19             these methods:
20              
21             =over
22              
23             =item * C
24              
25             =item * C
26              
27             =item * C
28              
29             =back
30              
31             … which behave as they normally do in promise implementations.
32              
33             Additionally, C and C may be used, thus:
34              
35             my $p3 = Promise::XS::Promise->all( $p1, $p2, .. );
36             my $p3 = Promise::XS::Promise->race( $p1, $p2, .. );
37              
38             … or, just:
39              
40             my $p3 = ref($p1)->all( $p1, $p2, .. );
41             my $p3 = ref($p1)->race( $p1, $p2, .. );
42              
43             … or even:
44              
45             my $p3 = $p1->all( $p1, $p2, .. );
46             my $p3 = $p1->race( $p1, $p2, .. );
47              
48             (Note the repetition of $p1 in these last examples!)
49              
50             =head1 NOTES
51              
52             Subclassing this class won’t work because the above-named methods always
53             return instances of (exactly) this class. That may change eventually,
54             but for now this is what’s what.
55              
56             =cut
57              
58             # Lifted from Promise::ES6
59             sub race {
60 2     2 0 139302 my $deferred = Promise::XS::Deferred::create();
61              
62 2         3 my $is_done;
63              
64 2         5 my $promise = $deferred->promise();
65              
66             my $on_resolve_cr = sub {
67 4 100   4   10 return if $is_done;
68 2         3 $is_done = 1;
69              
70 2         6 $deferred->resolve(@_);
71              
72             # Proactively eliminate references:
73 2         7 undef $deferred;
74 2         24 };
75              
76             my $on_reject_cr = sub {
77 0 0   0   0 return if $is_done;
78 0         0 $is_done = 1;
79              
80 0         0 $deferred->reject(@_);
81              
82             # Proactively eliminate references:
83 0         0 undef $deferred;
84 2         5 };
85              
86 2         6 for my $given_promise (@_[1 .. $#_]) {
87 4         12 $given_promise->then($on_resolve_cr, $on_reject_cr);
88             }
89              
90 2         15 return $promise;
91             }
92              
93             sub _warn_unhandled {
94 11     11   388435 my (@reasons) = @_;
95              
96 11         22 my $class = __PACKAGE__;
97              
98 11 50       38 if (1 == @reasons) {
99 11         124 warn "$class: Unhandled rejection: $reasons[0]\n";
100             }
101             else {
102 0           my $total = 0 + @reasons;
103              
104 0           for my $i ( 0 .. $#reasons ) {
105 0           my $num = 1 + $i;
106              
107 0           warn "$class: Unhandled rejection ($num of $total): $reasons[$i]\n";
108             }
109             }
110             }
111              
112             #----------------------------------------------------------------------
113             # Future::AsyncAwait interface
114             #----------------------------------------------------------------------
115              
116             #sub AWAIT_ON_READY {
117             # $_[0]->finally($_[1])->catch(\&AWAIT_CHAIN_CANCEL);
118             #}
119              
120             1;