Last active
August 29, 2015 14:25
-
-
Save mrenvoize/6720c93b169a255ef0e9 to your computer and use it in GitHub Desktop.
Mojolicious UA with Delay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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