Skip to content

Instantly share code, notes, and snippets.

@sirg
Created October 27, 2012 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirg/3966312 to your computer and use it in GitHub Desktop.
Save sirg/3966312 to your computer and use it in GitHub Desktop.
Don't get Mojo::IOLoop
#!/usr/bin/env perl
use Mojolicious::Lite;
sub gdone {
my $tx = shift;
$tx->res->dom->at('.section-en_us-tc .titletext')->text;
}
sub ydone {
my $tx = shift;
$tx->res->dom->at('.weather-data .temp-range')->text;
}
sub dchain {
my ($sub, $chain) = @_;
sub {
my ($delay, $arg) = @_;
my $res = $sub->($arg);
$delay->$chain($res);
}
}
get '/' => sub {
my $self = shift;
my $ua = app->ua;
Mojo::IOLoop->delay(
sub {
my $delay = shift;
$ua->get('www.yahoo.com' => dchain(\&ydone, $delay->begin));
$ua->get('news.google.com' => dchain(\&gdone, $delay->begin));
},
sub {
my ($d, $res_y, $res_g) = @_;
$self->render(text => "'$res_y' '$res_g'");
});
};
app->start;
Mojo::IOLoop already running at /home/sergey/perl5/perlbrew/perls/perl-5.16.1/lib/site_perl/5.16.1/Mojo/IOLoop/Delay.pm line 27.
@sirg
Copy link
Author

sirg commented Oct 27, 2012

Solution: use multiple steps as args to delay instead of calling wait().
Notice that post-processing can be chained onto completion of the action before calling the step completion callback. This allows complex/costly post-processing to be started before the next step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment