Created
April 6, 2011 15:05
-
-
Save esobchenko/905808 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Data::Dumper; | |
use List::Util qw/reduce/; | |
use Mojolicious::Lite; | |
use Mojo::UserAgent; | |
use Mojo::IOLoop; | |
get '/async' => sub { | |
my $self = shift; | |
my @res = (); | |
my $ua = $self->ua; | |
$ua->get( 'http://google.com' => cb( \@res ) ); | |
$ua->get( 'http://ya.ru' => cb( \@res ) ); | |
$ua->ioloop->start; | |
return $self->render( text => join "\n", @res ); | |
}; | |
app->start; | |
sub cb { | |
my $output = shift; | |
return sub { | |
my ( $ua, $tx ) = @_; | |
push @$output, $tx->req->url->to_string; | |
$ua->ioloop->stop if @$output >= 2; | |
}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment