Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added step example to cookbook
  • Loading branch information
kraih committed Aug 17, 2012
1 parent 4254c06 commit 92565c5
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -314,30 +314,30 @@ L<Mojo::IOLoop> delay.

use Mojolicious::Lite;
use Mojo::IOLoop;
use Mojo::URL;

# Search Twitter for "perl" and "python"
get '/' => sub {
my $self = shift;

# Delay rendering
my $delay = Mojo::IOLoop->delay(sub {
my ($delay, @results) = @_;
$self->render(json => {results => \@results});
});

# First request
$delay->begin;
$self->ua->get('http://search.twitter.com/search.json?q=perl' => sub {
my ($ua, $tx) = @_;
$delay->end($tx->res->json->{results}[0]{text});
});

# Second request
$delay->begin;
$self->ua->get('http://search.twitter.com/search.json?q=python' => sub {
my ($ua, $tx) = @_;
$delay->end($tx->res->json->{results}[0]{text});
});
# Prepare response in two steps
Mojo::IOLoop->delay(

# Parallel requests
sub {
my $delay = shift;
my $url = Mojo::URL->new('http://search.twitter.com/search.json');
$self->ua->get($url->clone->query({q => 'perl'}) => $delay->begin);
$self->ua->get($url->clone->query({q => 'python'}) => $delay->begin);
},

# Delayed rendering
sub {
my $delay = shift;
my @results = map { $_->res->json->{results}[0]{text} } @_;
$self->render(json => {results => \@results});
}
);
};

app->start;
Expand Down

0 comments on commit 92565c5

Please sign in to comment.