| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mojo::Promise::Role::Get; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
753
|
use Carp (); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
26
|
|
|
4
|
1
|
|
|
1
|
|
5
|
use Role::Tiny; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = 'v0.1.1'; |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
requires qw(ioloop then wait); |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub get { |
|
11
|
19
|
|
|
19
|
1
|
30625
|
my ($self) = @_; |
|
12
|
19
|
100
|
|
|
|
45
|
Carp::croak "'get' cannot be called when the event loop is running" if $self->ioloop->is_running; |
|
13
|
18
|
|
|
|
|
322
|
my (@result, $rejected); |
|
14
|
18
|
|
|
12
|
|
100
|
$self->then(sub { @result = @_ }, sub { $rejected = 1; @result = @_ })->wait; |
|
|
12
|
|
|
|
|
64035
|
|
|
|
6
|
|
|
|
|
52041
|
|
|
|
6
|
|
|
|
|
18
|
|
|
15
|
18
|
100
|
|
|
|
3885
|
if ($rejected) { |
|
16
|
6
|
|
100
|
|
|
20
|
my $reason = $result[0] // 'Promise was rejected'; |
|
17
|
6
|
50
|
33
|
|
|
39
|
die $reason if ref $reason or $reason =~ m/\n\z/; |
|
18
|
6
|
|
|
|
|
593
|
Carp::croak $reason; |
|
19
|
|
|
|
|
|
|
} |
|
20
|
12
|
100
|
|
|
|
114
|
return wantarray ? @result : $result[0]; |
|
21
|
|
|
|
|
|
|
} |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
1; |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 NAME |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Mojo::Promise::Role::Get - Wait for the results of a Mojo::Promise |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
use Mojo::IOLoop; |
|
32
|
|
|
|
|
|
|
use Mojo::Promise; |
|
33
|
|
|
|
|
|
|
use Mojo::UserAgent; |
|
34
|
|
|
|
|
|
|
my $ua = Mojo::UserAgent->new; |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# long way of writing $ua->get('http://example.com')->result |
|
37
|
|
|
|
|
|
|
my $res = $ua->get_p('http://example.com')->with_roles('+Get')->get->result; |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# wait for multiple requests at once |
|
40
|
|
|
|
|
|
|
my @responses = map { $_->[0]->result } Mojo::Promise->all( |
|
41
|
|
|
|
|
|
|
$ua->get_p('http://example.com'), |
|
42
|
|
|
|
|
|
|
$ua->get_p('https://www.google.com'), |
|
43
|
|
|
|
|
|
|
)->with_roles('Mojo::Promise::Role::Get')->get; |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# request with exception on timeout |
|
46
|
|
|
|
|
|
|
my $timeout = Mojo::Promise->new; |
|
47
|
|
|
|
|
|
|
Mojo::IOLoop->timer(1 => sub { $timeout->reject('Timed out!') }); |
|
48
|
|
|
|
|
|
|
my $res = Mojo::Promise->race($ua->get_p('http://example.com'), $timeout) |
|
49
|
|
|
|
|
|
|
->with_roles('Mojo::Promise::Role::Get')->get->result; |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
L is a L L that adds |
|
54
|
|
|
|
|
|
|
a L"get"> method to facilitate the usage of asynchronous code in a |
|
55
|
|
|
|
|
|
|
synchronous manner, similar to L. |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
Note: Like in Future, L"get"> cannot retrieve results when the event loop is |
|
58
|
|
|
|
|
|
|
already running, as that can recurse into the event reactor. Unlike in Future, |
|
59
|
|
|
|
|
|
|
this is true even if the promise has already been resolved or rejected, because |
|
60
|
|
|
|
|
|
|
retrieving L results always requires running the event loop. |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 METHODS |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
L composes the following methods. |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head2 get |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my @results = $promise->get; |
|
69
|
|
|
|
|
|
|
my $first_result = $promise->get; |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Blocks until the promise resolves or is rejected. If it is fulfilled, the |
|
72
|
|
|
|
|
|
|
results are returned. In scalar context the first value is returned. If the |
|
73
|
|
|
|
|
|
|
promise is rejected, the (first value of the) rejection reason is thrown as an |
|
74
|
|
|
|
|
|
|
exception. |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
An exception is thrown if the L is running, to prevent |
|
77
|
|
|
|
|
|
|
recursing into the event reactor. |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 BUGS |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Report any issues on the public bugtracker. |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 AUTHOR |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Dan Book |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
This software is Copyright (c) 2018 by Dan Book. |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
This is free software, licensed under: |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
L |