AnyEvent-Future

 view release on metacpan or  search on metacpan

lib/AnyEvent/Future.pm  view on Meta::CPAN

method to block until the future is ready. It allows C<AnyEvent>-using code to
be written that returns C<Future> instances, so that it can make full use of
C<Future>'s abilities, including L<Future::Utils>, and also that modules using
it can provide a C<Future>-based asynchronous interface of their own.

For a full description on how to use Futures, see the L<Future> documentation.

=cut

# Forward
sub as_future(&);

=head1 CONSTRUCTORS

=cut

=head2 new

   $f = AnyEvent::Future->new

Returns a new leaf future instance, which will allow waiting for its result to

lib/AnyEvent/Future.pm  view on Meta::CPAN


   $w = CODE->( $f )

This utility is provided for the common case of wanting to wrap an C<AnyEvent>
function which will want to receive a callback function to inform of
completion, and which will return a watcher object reference that needs to be
stored somewhere.

=cut

sub as_future(&)
{
   my ( $code ) = @_;

   my $f = AnyEvent::Future->new;

   $f->set_udata( w => scalar $code->( $f ) );
   $f->on_cancel( sub { $f->set_udata( w => undef ) } );

   return $f;
}

lib/AnyEvent/Future.pm  view on Meta::CPAN


A futher shortcut to C<as_future>, where the code is passed two callback
functions for C<done> and C<fail> directly, avoiding boilerplate in the common
case for creating these closures capturing the future variable. In many cases
this can reduce the code block to a single line.

 $w = CODE->( $done_cb, $fail_cb )

=cut

sub as_future_cb(&)
{
   my ( $code ) = @_;

   &as_future( sub {
      my $f = shift;
      $code->( sub { $f->done(@_) }, sub { $f->fail(@_) } );
   });
}

=head1 EXAMPLES



( run in 1.142 second using v1.01-cache-2.11-cpan-65fba6d93b7 )