Mojolicious::Plugin::Future - use Future in Mojolicious applications
use Mojolicious::Lite; plugin 'Future'; use Scalar::Util 'weaken'; use Mojo::UserAgent; my $ua = Mojo::UserAgent->new; get '/async_callback' => sub { my $c = shift; my $f = $c->future; weaken(my $weak_f = $f); # only close over weakened Future $ua->get('http://example.com', sub { my ($ua, $tx) = @_; $tx->success ? $weak_f->done($tx->result) : $weak_f->fail($tx->error->{message}); }); $c->adopt_future($f->on_done(sub { my ($result) = @_; $c->render(json => {result => $result->text}); })); }; get '/future_returning' => sub { my $c = shift; $c->adopt_future(returns_a_future()->then(sub { my @result = @_; return returns_another_future(@result); })->on_done(sub { my @result = @_; $c->render(json => {result => \@result}); })); };
Mojolicious::Plugin::Future is a convenient way to use Future in a Mojolicious application. The final future in a sequence or convergence is passed to the "adopt_future" helper, which takes care of the details of asynchronous rendering in a similar fashion to "delay" in Mojolicious::Plugin::DefaultHelpers.
$f = $c->adopt_future($f);
Disables automatic rendering, stores the Future instance, keeps a reference to "tx" in Mojolicious::Controller in case the underlying connection gets closed early, and calls "reply->exception" in Mojolicious::Plugin::DefaultHelpers if the Future fails.
my $f = $c->future; my $f = $c->future($loop);
Convenience method to return a new Future::Mojo object.
Mojolicious::Plugin::Future inherits all methods from Mojolicious::Plugin and implements the following new ones.
$plugin->register(Mojolicious->new);
Register helper in Mojolicious application.
Report any issues on the public bugtracker.
Dan Book <dbook@cpan.org>
This software is Copyright (c) 2017 by Dan Book.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
Future::Mojo, Future
To install Mojolicious::Plugin::Future, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::Future
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::Future
For more information on module installation, please visit the detailed CPAN module installation guide.