It cannot return to the same method, if that's what you're asking. Any
action you wish to take using the results must be within that callback, or
called by that callback; so perhaps pass a callback to your model for it to
call from its callback, if that makes sense.
https://metacpan.org/pod/Mojolicious::Plugin::DefaultHelpers#delay can help
with this in the controller method.

On Fri, May 5, 2017 at 3:30 AM, Stefan Adams <[email protected]> wrote:

> I have a question about Mojo::Pg's non-blocking mode and the best way to
> refer to it is using Mojo::Pg's example Blog application Model class
> <https://github.com/kraih/mojo-pg/blob/master/examples/blog/lib/Blog/Model/Posts.pm>
> .
>
> find(), e.g., fetches some data from a database.  What if that find
> operation is two seconds -- the current example code is a blocking
> operation and therefore the Mojo::Server process will be unable to respond
> to other HTTP requests while the current request waits for two seconds to
> get its data from the database.  Is that correct?
>
> So, instead, if the call were non-blocking, then while the Database takes
> its two seconds to fetch the requested data, the Mojo::Server process can
> go back to handling other HTTP requests.  Is that correct?
>
> Assuming these are correct, my question is, how can I return the data
> retrieved from the database to the Controller?
>
>
> package Model;
>
> use Mojo::Base -base;
>
> sub find {
>   my ($self, $id) = @_;
>   $self->pg->db->select('posts', undef, {id => $id} => sub {
>
>     my ($db, $err, $results) = @_
>
>     return $results->hashes->to_array; *# How to have find() return
> this??*
>
>   });
> }
>
> package Controller:
>
> use Mojo::Base 'Mojolicious::Controller';
>
> sub index {
>
>   my $self = shift;
>
>   $self->render(json => $self->model->find($self->param('id')));
> }
>
>
>
> --
> 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.

Reply via email to