AnyEvent-XSPromises

 view release on metacpan or  search on metacpan

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

Rejects the deferred object (assigns a reason for why it failed). All associated promises will have their callback invoked
in the next event loop iteration.

=item $d->is_in_progress()

Returns true iff the C<reject> or C<resolve> method has not been called yet. Useful for racing multiple code paths to
resolve/reject a single deferred object, like one would do to build a timeout.

    sub get_with_timeout {
        my $d= deferred;
        my $timer; $timer= AE::timer 1, 0, sub {
            undef $timer;
            $d->reject("Timed out") if $d->is_in_progress;
        };

        http_get("https://perl.org", sub {
            my $result= shift
            $d->resolve($result) if $d->is_in_progress;
        });

This method is intentionally not available on promise objects.

lib/AnyEvent/XSPromises/Loader.pm  view on Meta::CPAN

        1;
    } or do {
        my $error= $@;
        if (!$called++) {
            $deferred->reject($error);
        }
    };
    return $deferred->promise;
});

# We do not use AE::postpone, because it sets a timer of 0 seconds. While that sounds great in
# theory, the underlying libraries (eg. epoll, used by EV) don't support 0 second timers, and
# so they get passed 1ms instead. To avoid actually waiting a millisecond every time, we write
# data onto a socket read by the event loop. Of course, these sockets need to be carefully managed
# in case the code does a fork, so we need to frequently check $$.
my ($AE_PID, $AE_WATCH, $PIPE_IN, $PIPE_OUT);
BEGIN { $AE_PID= -1; }

sub ___notify_callback {
    if ($$ != $AE_PID) {
        ___reset_pipe();
    } else {

t/aplus.t  view on Meta::CPAN

use 5.010;
use strict;
use warnings;

use Test::More;
use AnyEvent;
use AnyEvent::XSPromises qw/deferred resolved rejected collect/;

sub delayed {
    my ($seconds, $sub)= @_;
    my $timer; $timer= AE::timer $seconds, 0, sub {
        undef $timer;
        &$sub;
    };
}
sub expect_resolve {
    $_[0]->then(sub {
        1;
    }, sub {
        fail;
    });
}



( run in 1.071 second using v1.01-cache-2.11-cpan-49f99fa48dc )