File Coverage

blib/lib/Mojo/Promise/Role/Any.pm
Criterion Covered Total %
statement 16 16 100.0
branch 4 4 100.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 26 26 100.0


line stmt bran cond sub pod time code
1             package Mojo::Promise::Role::Any;
2 2     2   2159 use Mojo::Base '-role';
  2         196574  
  2         15  
3              
4 2     2   958 use strict;
  2         6  
  2         579  
5              
6             our $VERSION = '1.005';
7              
8             =encoding utf8
9              
10             =head1 NAME
11              
12             Mojo::Promise::Role::Any - Fulfill with the first fulfilled promise
13              
14             =head1 SYNOPSIS
15              
16             use Mojo::Promise;
17              
18             my $any_promise = Mojo::Promise
19             ->with_roles( '+Any' )
20             ->any( @promises );
21              
22             =head1 DESCRIPTION
23              
24             NOTE: Mojolicious 8.73 adds C as a stable feature, so you don't
25             need this role.
26              
27             Make a new promise that fulfills with the first fulfilled promise, and
28             rejects otherwise. The result is a flat list of the arguments for the
29             fulfilled promise (and not an anonymous array of values).
30              
31             This should be the Perl expression of the same idea in bluebirdjs
32             (L).
33              
34             This is handy, for instance, for asking for several servers to provide
35             the same resource and taking the first one that responds.
36              
37             =over 4
38              
39             =item any( @promises )
40              
41             Takes a lists of promises (or thenables) and returns another promise
42             that fulfills when any promise fulfills (and it ignores the
43             others after that).
44              
45             If none of the promises fulfill, the any promise rejects.
46              
47             If you pass no promises, the any promise rejects.
48              
49             =cut
50              
51             sub any {
52 6     6 1 26863 my( $self, @promises ) = @_;
53 6         21 my $any = $self->new;
54              
55 6         156 my $count = 0;
56             $_->then(
57 9     9   2047 sub { $any->resolve( @_ ); return },
  9         417  
58 6 100   6   1334 sub { $count++; $any->reject if $count == @promises; return }
  6         23  
  6         58  
59 6         37 ) foreach @promises;
60              
61 6 100       4052 return @promises ? $any : $any->reject;
62             }
63              
64             =back
65              
66             =head1 SEE ALSO
67              
68             L, L, L
69              
70             L
71              
72             =head1 SOURCE AVAILABILITY
73              
74             This source is in Github:
75              
76             https://github.com/briandfoy/mojo-promise-role-higherorder
77              
78             =head1 AUTHOR
79              
80             brian d foy, C<< >>
81              
82             =head1 COPYRIGHT AND LICENSE
83              
84             Copyright © 2018-2021, brian d foy, All Rights Reserved.
85              
86             You may redistribute this under the terms of the Artistic License 2.0.
87              
88             =cut
89              
90             1;