Subject: | Mojo::UserAgent example for Promises::Cookbook::TIMTOWTDI |
#!/usr/bin/perl
use Mojo::Base -strict;
use Mojo::UserAgent;
my $titles;
my $ua = Mojo::UserAgent->new;
Mojo::IOLoop->delay(
sub {
my $delay = shift;
$ua->get('http://google.com/', $delay->begin);
$ua->get('http://yahoo.com/', $delay->begin);
$ua->get('http://perlmonks.org/', $delay->begin);
},
sub {
my ($delay, $tx1, $tx2, $tx3) = @_;
$titles = {
google => $tx1->res->dom->at('title')->text,
yahoo => $tx2->res->dom->at('title')->text,
perlmonks => $tx3->res->dom->at('title')->text,
};
},
)->catch(
sub {
my ($delay, $err) = @_;
warn "failed to download or parse title\n";
}
)->wait;
say Mojo::Util::dumper($titles);