Small note, in a Mojolicious::Lite app you are not operating in the controller class, so the controller would not be referred to as $self, by matter of convention (usually $c).
On Fri, Jan 22, 2016 at 9:29 AM, Dan Book <[email protected]> wrote: > When you moved the helper, you moved your render outside of the callback, > so it's called immediately rather than when the actions are done. Also, you > cannot return values from the delay back to where the helper was called, > because the helper returns immediately, the delay happens later. You can > make your helper take a callback and call that from the last step of the > delay. > > my ($self, $params, $cb) = @_; > ... > sub { > my $delay = shift; > my $req2 = ... > $self->$cb($req2); > }); > # here is where you can return something immediately from the helper, > before the delay runs > > Then you would call it like this: > > $self->a_proxy( { ... }, sub { > my ($self, $req2) = @_; > $self->render(json => $req2); > }); > > On Fri, Jan 22, 2016 at 8:52 AM, Pavel Serikov <[email protected]> > wrote: > >> I need to send one request, then wait 10 seconds, then send another >> request, then render result. >> Assume that I need to use Mojo::IOLoop for that (please correct me if >> there are other options). >> >> If I do it inside route everything works fine: >> get '/a' => sub { >> my $self = shift; >> my $req1 = $self->ua->post(...)->res; >> Mojo::IOLoop->timer(10 => sub { >> my $req2 = $self->ua->get('...'.$req1)->res; >> $self->render(json => $req2); >> }); >> }; >> >> But if move this code to helper it doesn't work, Mojo render result >> immediately. >> helper a_proxy => sub { >> my ($self, $params) = @_; >> my $delay = Mojo::IOLoop->delay( >> sub { >> my $delay = shift; >> my $req1 = $self->ua->post('...' => $delay->begin); >> }, >> sub { >> my ($delay, $tx) = @_; >> $delay->data({id => $tx->res}); >> Mojo::IOLoop->timer(10 => $delay->begin); >> }, >> sub { >> my $delay = shift; >> my $req2 = $self->ua->get('...'.$delay->data->{id})->res; >> return $req2; >> } >> ); >> }; >> >> .. >> >> get '/a' => sub { >> my $self = shift; >> my $r = $self->a_proxy( { ... } ); >> $self->render(json => $r); >> }; >> >> What am I doing 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. >> > > -- 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.
