sectokia has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks,

AnyEvent::HTTPD has request callbacks for when a request comes in. These requests must be processed and the results returned by the callback.

My Question is: What if to handle the request, I need to in-turn perform an async operation, such as an AnyEvent::HTTP request to another server. How can I achieve this?

It doesn't seem there is anyway to just return in the callback, and call HTTPD later when I have a response for the request?

  • Comment on AnyEvent::HTTPD -> async response generation

Replies are listed 'Best First'.
Re: AnyEvent::HTTPD -> async response generation
by Corion (Patriarch) on Mar 12, 2021 at 06:16 UTC

    Take a look at the test suite, in t/06_long_resp.t there is the approach on how to generate a response using multiple callbacks:

    $h->reg_cb ( '/test' => sub { my ($httpd, $req) = @_; $req->respond ({ content => ['text/plain', sub { my ($data_cb) = @_; return unless $data_cb; $data_cb->(substr $SENT, 0, 10, ''); }] }); }, );

    Basically, it is the same approach that Plack uses. Your ->respond call passes in a subroutine, that gets another callback as a parameter. You then call this callback every time you have more content for the response.