I misprinted, at blocking Test.pm there is 

return $ua->get($api_url => form => \%hash)->res->*json*

But it doesn't affect on problem.

вторник, 5 апреля 2016 г., 19:14:37 UTC+4 пользователь Pavel Serikov 
написал:
>
> I want to make non-blocking requests to API via Mojo::UserAgent and allow 
> to render result of request without waiting result of previous request.
> Code of working with API made in separate module (listing below). How to 
> convert code to non-blocking way?
>
> *$cat Test.pm*
> ...
> my $ua = Mojo::UserAgent->new;
> sub api_func1 {
> return $ua->get($api_url => form => \%hash)->res->body;
> } 
> ...
> *$cat myapp.pl <http://myapp.pl>*
> ...
> get '/r' => sub {
> my $c = shift;
> $c->render(json => api_func1());
> }
> ...
>
> I rewrote it to non-blocking way:
>
> *$cat Test.pm*
> sub api_func1 {
> my $cb = shift;
> $ua->get($api_url => form => \%hash => sub {
>     my ($ua, $tx) = @_;
>     *$cb**($tx->res->json);*
>   });
> Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
> };
> *$cat myapp.pl <http://myapp.pl>*
> get '/r' => sub {
> my $c = shift;
> api_func1(sub => {
>  my ($self, $json) = @_;
>  $c->render(json => $json);
> })
>
> But I got a syntax error at string marked orange. What is wrong?
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to