Skip to content

Instantly share code, notes, and snippets.

@yko
Forked from esobchenko/mojo_async.pl
Created April 6, 2011 15:36
Show Gist options
  • Save yko/905869 to your computer and use it in GitHub Desktop.
Save yko/905869 to your computer and use it in GitHub Desktop.
#!/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 $ua = $self->ua; $ua->ioloop(Mojo::IOLoop->singleton); $ua->get('http://google.com' => cb($self)); $ua->get('http://ya.ru' => cb($self)); $self->render_later; }; sub cb { my $c = shift; my $output = \@{$c->{_output}}; return sub { my ( $ua, $tx ) = @_; push @$output, $tx->req->url->to_string;
if (@$output >= 2) { $c->render(text => join "\n", @$output); } }; } app->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment