File Coverage

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


line stmt bran cond sub pod time code
1             package Mojo::Promise::Role::Any;
2 2     2   1924 use Mojo::Base '-role';
  2         135051  
  2         17  
3              
4 2     2   919 use strict;
  2         5  
  2         354  
5              
6             our $VERSION = '1.002';
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 28081 my( $self, @promises ) = @_;
50 6         22 my $any = $self->new;
51              
52 6     9   60 $_->then( sub { $any->resolve( @_ ) } ) foreach @promises;
  9         2093  
53              
54 6 100       2380 return @promises ? $any : $any->reject;
55             }
56              
57             =back
58              
59             =head1 SEE ALSO
60              
61             L, L, L
62              
63             L
64              
65             =head1 SOURCE AVAILABILITY
66              
67             This source is in Github:
68              
69             https://github.com/briandfoy/mojo-promise-role-higherorder
70              
71             =head1 AUTHOR
72              
73             brian d foy, C<< >>
74              
75             =head1 COPYRIGHT AND LICENSE
76              
77             Copyright (c) 2018, brian d foy, All Rights Reserved.
78              
79             You may redistribute this under the terms of the Artistic License 2.0.
80              
81             =cut
82              
83             1;