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