Skip to content

Commit

Permalink
better examples for delays
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 30, 2014
1 parent 153e22d commit d55ca4f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

5.11 2014-06-29
5.11 2014-06-30
- Improved error method in Mojolicious::Validator::Validation to return
field names when called without arguments.

Expand Down
5 changes: 4 additions & 1 deletion lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -405,7 +405,7 @@ often result from continuation-passing style.
my $c = shift;

# Prepare response in two steps
Mojo::IOLoop->delay(
my $delay = Mojo::IOLoop->delay(

# Concurrent requests
sub {
Expand All @@ -425,6 +425,9 @@ often result from continuation-passing style.
});
}
);

# Handle exceptions in steps and start event loop if necessary
$delay->catch(sub { $c->render_exception(pop) })->wait;
};

app->start;
Expand Down
25 changes: 14 additions & 11 deletions lib/Mojolicious/Lite.pm
Expand Up @@ -876,17 +876,20 @@ L<Mojo::JSON> and L<Mojo::DOM> this can be a very powerful tool.
# Concurrent non-blocking
get '/titles' => sub {
my $c = shift;
my $delay = Mojo::IOLoop->delay(sub {
my ($delay, @titles) = @_;
$c->render(json => \@titles);
});
for my $url ('http://mojolicio.us', 'https://metacpan.org') {
my $end = $delay->begin(0);
$c->ua->get($url => sub {
my ($ua, $tx) = @_;
$end->($tx->res->dom->html->head->title->text);
});
}
Mojo::IOLoop->delay(
sub {
my $delay = shift;
$c->ua->get('http://mojolicio.us' => $delay->begin);
$c->ua->get('https://metacpan.org' => $delay->begin);
},
sub {
my ($delay, $mojo, $cpan) = @_;
$c->render(json => {
mojo => $mojo->res->dom->html->head->title->text,
cpan => $cpan->res->dom->html->head->title->text
});
}
);
};
app->start;
Expand Down

0 comments on commit d55ca4f

Please sign in to comment.