AnyEvent-Promises

 view release on metacpan or  search on metacpan

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

If the C<$p> is fulfilled, then $on_fulfilled handler is called with the values 
of C<$p> as an arguments.

If the C<$p> is rejected, then $on_rejected handler is called with the
rejection reason of C<$p> as an argument.

The handler (either C<$on_fulfilled> or C<$on_rejected>) is called in
a list context so it can return multiple values (here it differs from JavaScript
implementation). 

If the handler throws an exception, then C<$pp> is rejected with the
exception.

If the handler does not throw an exception and does not return a
promise, then C<$pp> is fulfilled with the values returned by the handler.

If the handler returns a promise, then C<$pp> is fulfilled/rejected
when the promise returned is fulfilled/rejected with the same values/reason.

It must be stressed that any handler is called outside of current stack 
in the "next tick" of even loop using C<< AnyEvent->postpone >>. 
It implies that without an event loop running now or later the handler is never called.

See example:

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

            warn "Code here is called when using AnyEvent::Promises";
        },
        sub {
            warn "Code here is called when using Promises";
        }
    );

With C<Promises> the C<$p> promise is finally rejected with C<$reason>,
while with C<AnyEvent::Promises> the C<$promise> is finally fulfilled
with C<$reason>, because the exception was handled (the handler did not
throw an exception).

=item L<https://github.com/kriskowal/q/wiki/API-Reference>

Here I shamelessly copied the ideas from.

=back

=head1 AUTHOR

Roman Daniel <roman.daniel@davosro.cz>

t/03-make_promise.t  view on Meta::CPAN

            );
            ok( $p1->is_fulfilled,
                'promise created from value is fulfilled' );
            ok( $p2->is_pending,
                'promise from code is pending, code was not evaluated yet' );
            my $w; $w = AE::timer(0,0, sub {undef $w; $cv->send });
        }
    );
};

subtest make_promise_with_throw => sub {
    my @data;
    run_event_loop(
        sub {
            my $cv = shift;
            my $p = make_promise( sub {die "oops\n"} );
            ok( $p->is_pending,
                'promise from code is pending, code was not evaluated yet' );
            $p->then(sub {
                fail("promise is rejected");
            }, sub {

t/05-sync.t  view on Meta::CPAN

        [ $d->promise->sync ],
        [ 4, 3, 2 ],
        "sync on delayed fulfilled promise"
    );
}

for my $d ( deferred() ) {
    my $reason = "Oops, something got terribly wrong\n";
    $d->reject($reason);

    throws_ok {
        $d->promise->sync; 
    } qr{^\Q$reason}, "sync on rejected promise throws the reason";
}

for my $timeout ( undef, 2 ) {
    my $d = deferred();

    my @trace;
    my @tm = map {
        my $delay = $_;
        AE::timer $delay, 0, sub { push @trace, $delay }
    } 1, 3, 6;

    throws_ok {
        $d->promise->sync( $timeout || () );
    }
    qr{TIMEOUT\n}, "sync on pending promise (TIMEOUT)";

    if ($timeout) {
        is_deeply( \@trace, [1], "timeout can be set" );
    }
    else {
        is_deeply( \@trace, [ 1, 3 ], "default timeout is 5 seconds" );
    }



( run in 0.509 second using v1.01-cache-2.11-cpan-496ff517765 )