The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojolicious::Plugin::Future - use Future in Mojolicious applications

SYNOPSIS

 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});
   }));
 };

DESCRIPTION

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.

HELPERS

adopt_future

  $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.

future

  my $f = $c->future;
  my $f = $c->future($loop);

Convenience method to return a new Future::Mojo object.

METHODS

Mojolicious::Plugin::Future inherits all methods from Mojolicious::Plugin and implements the following new ones.

register

  $plugin->register(Mojolicious->new);

Register helper in Mojolicious application.

BUGS

Report any issues on the public bugtracker.

AUTHOR

Dan Book <dbook@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2017 by Dan Book.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

SEE ALSO

Future::Mojo, Future