mojo async/await with controller

252 views
Skip to first unread message

Tobias Oetiker

unread,
Jan 10, 2020, 8:49:56 AM1/10/20
to Mojolicious
I just read Joel's blog (https://mojolicious.io/blog/2018/12/24/async-await-the-mojo-way/) about async/await support. This is wonderful. I am totally excited about this. Thank you to all involved!

Obviously I had to test this right away and it works great with the Mojolicious::Lite demo shown in the blog. 

But how do I specify a route to a async method in a controller for a Mojolicious app?

Using a callback I can easily translate the Lite approach

$r->get('/cb' => async sub ($c) {
    await
Mojo::Promise->new(sub ($resolve,$fail) {
     
Mojo::IOLoop->timer(3 => sub {
          $resolve
->();
     
});
   
});
    $c
->render(text => 'hello');
});

but how do I use this when I want to have the code in a controller:

$r->get('/ctr')->to('example#hello');

with

package MyApp::Controller::Example;
use Mojo::Base 'Mojolicious::Controller', -async, -signatures;

async
sub welcome ($self) {
  await
Mojo::Promise->new(sub ($resolve,$fail) {
     
Mojo::IOLoop->timer(3 => sub {
          $resolve
->();
     
});
 
});
  $self
->render(text => 'hello');
}

does not work as expected as the router does not seem to expect the methods to be async when called like that.

Tobias Oetiker

unread,
Jan 10, 2020, 9:06:27 AM1/10/20
to Mojolicious
ps. it also does not work when the method in the controller is called hello ...

Felipe Gasper

unread,
Jan 10, 2020, 9:08:22 AM1/10/20
to mojol...@googlegroups.com
I’m just encountering Mojo::AsyncAwait and notice that it lists Coro as a dependency.

Isn’t Coro problematic in newer Perls? I thought Marc had basically written off support for 5.22 onward.

-F
> --
> 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 mojolicious...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/519ec59d-cb9d-46c6-9a6a-970a2f3142b6%40googlegroups.com.

Tobias Oetiker

unread,
Jan 10, 2020, 9:19:24 AM1/10/20
to Mojolicious
it does not depend on coro anymore ... read the blog post I cited!

Tobias Oetiker

unread,
Jan 10, 2020, 9:20:56 AM1/10/20
to Mojolicious
aargh I cited the wrong one :) way to excited just now :)

Tobias Oetiker

unread,
Jan 10, 2020, 9:28:55 AM1/10/20
to Mojolicious
I am answering my own questions here ... so it seems I still have to call render_later in the controller to make this work ..

async sub hello ($self) {
 
# Render template "example/welcome.html.ep" with message
 
$self->render_later;

Jeyaraj Durairaj

unread,
May 26, 2020, 2:20:29 PM5/26/20
to Mojolicious
Hi Tobias,

I am still getting the following.

Note: my router is   $r->post('/login')->to('sessions#on_user_login_p')->name('do_login');

[2020-05-26 23:47:28.06952] [-19048] [debug] Routing to controller "ConvergeAtCommex::Controller::Sessions" and action "on_user_login_p"
[2020-05-26 23:47:28.06985] [-19048] [debug] Action not found in controller

Sebastian Riedel

unread,
May 26, 2020, 2:51:34 PM5/26/20
to Mojolicious
I am still getting the following.

Note: my router is   $r->post('/login')->to('sessions#on_user_login_p')->name('do_login');

[2020-05-26 23:47:28.06952] [-19048] [debug] Routing to controller "ConvergeAtCommex::Controller::Sessions" and action "on_user_login_p"
[2020-05-26 23:47:28.06985] [-19048] [debug] Action not found in controller

That information seems completely unrelated to the original question in this thread. Please refrain from necroing.

--
sebastian 

Jeyaraj Durairaj

unread,
May 26, 2020, 11:14:00 PM5/26/20
to Mojolicious
Sure sir.
Reply all
Reply to author
Forward
0 new messages