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.

Reply via email to