File Coverage

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


line stmt bran cond sub pod time code
1             package Mojo::Promise::Role::Any;
2 2     2   2034 use Mojo::Base '-role';
  2         186806  
  2         15  
3              
4 2     2   926 use strict;
  2         4  
  2         441  
5              
6             our $VERSION = '1.003';
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             Make a new promise that fulfills with the first fulfilled promise, and
25             rejects otherwise. The result is a flat list of the arguments for the
26             fulfilled promise (and not an anonymous array of values).
27              
28             This should be the Perl expression of the same idea in bluebirdjs
29             (L).
30              
31             This is handy, for instance, for asking for several servers to provide
32             the same resource and taking the first one that responds.
33              
34             =over 4
35              
36             =item any( @promises )
37              
38             Takes a lists of promises (or thenables) and returns another promise
39             that fulfills when any promise fulfills (and it ignores the
40             others after that).
41              
42             If none of the promises fulfill, the any promise rejects.
43              
44             If you pass no promises, the any promise rejects.
45              
46             =cut
47              
48             sub any {
49 6     6 1 29905 my( $self, @promises ) = @_;
50 6         21 my $any = $self->new;
51              
52 6         159 my $count = 0;
53             $_->then(
54 9     9   2372 sub { $any->resolve( @_ ) },
55 6 100   6   1333 sub { $count++; $any->reject if $count == @promises }
  6         31  
56 6         35 ) foreach @promises;
57              
58 6 100       3909 return @promises ? $any : $any->reject;
59             }
60              
61             =back
62              
63             =head1 SEE ALSO
64              
65             L, L, L
66              
67             L
68              
69             =head1 SOURCE AVAILABILITY
70              
71             This source is in Github:
72              
73             https://github.com/briandfoy/mojo-promise-role-higherorder
74              
75             =head1 AUTHOR
76              
77             brian d foy, C<< >>
78              
79             =head1 COPYRIGHT AND LICENSE
80              
81             Copyright © 2018-2021, brian d foy, All Rights Reserved.
82              
83             You may redistribute this under the terms of the Artistic License 2.0.
84              
85             =cut
86              
87             1;