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.