Skip to content

Instantly share code, notes, and snippets.

@mrenvoize
Last active August 29, 2015 14:25
Show Gist options
  • Save mrenvoize/6720c93b169a255ef0e9 to your computer and use it in GitHub Desktop.
Save mrenvoize/6720c93b169a255ef0e9 to your computer and use it in GitHub Desktop.
Mojolicious UA with Delay
my $delay = Mojo::IOLoop->delay(
sub {
my $delay = shift;
$c->app->ua->get( $request, $delay->begin );
},
sub {
my ( $delay, $tx ) = @_;
die $tx->error->{message} if $tx->error;
my $json = $tx->success->json;
$c->$cb( undef, $json );
}
)->catch( sub { $c->cb( $_[1] ) } )->wait;
$c->app->ua->get(
$request => sub {
my ( $ua, $tx ) = @_;
if ( my $res = $tx->success ) {
my $json = $res->json;
return $json;
}
else {
my $err = $tx->error;
die "$err->{code} response: $err->{message}"
if $err->{code};
die "Connection error: $err->{message}";
}
}
);
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment