AnyEvent-Promises
view release on metacpan or search on metacpan
lib/AnyEvent/Promises.pm view on Meta::CPAN
=item C<promise>
Returns the promise for the deferred object.
=item C<resolve(@values)>
Resolve the deferred object with values. The argument list may be empty.
=item C<reject($reason)>
Reject the deferred object with a reason (exception). The C<$reason>
argument is required and must be true (in Perl sense).
A deferred object can be resolved or rejected once only.
Any subsequent call of C<resolve> or C<reject> is silently ignored.
=back
=head2 Methods of promise
The promise object is a consumer part of deferred object.
Each promise has an underlying deferred object.
The promise is fulfilled when C<resolve> was called on the underlying deferred object.
The values of promise are simply the arguments of C<< $deferred->resolve >>.
The promise is rejected when C<reject> was called on the underlying deferred object.
The reason of promise is simply the argument of C<< $deferred->reject >>.
=over 4
=item C<then($on_fulfilled, $on_rejected)>
The basic method of a promise. This method returns a new promise.
Each of C<$on_fulfilled> and C<$on_rejected> arguments is either coderef or undef.
my $pp = $p->then($on_fulfilled, $on_rejected);
The C<$pp> is fulfilled or rejected after C<$p> is fulfilled or rejected
according to following rules:
If the C<$p> is fulfilled and $on_fulfilled is not a coderef (it is undef,
another value has no meaning), then C<$pp> is fulfilled with the same values
as C<$p>.
If the C<$p> is rejected and $on_rejected is not a coderef (it is undef,
another value has no meaning), then C<$pp> is rejected with the same reason
as C<$p>.
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:
my $d = deferred();
$d->resolve(10);
my $p = $d->promise->then(sub { 2 * shift() });
warn $p->state; # yields 'pending' because the handler is yet to be called
warn $p->value; # yield undef for the same reason
The behaviour of C<then> in JavaScript is more precisely described
here: L<http://promises-aplus.github.io/promises-spec/#the__method>.
=item C<sync([$timeout])>
use AnyEvent::Promises qw(make_promise deferred);
make_promise(8)->sync; # returns 8
make_promise(sub { die "Oops" })->sync; # dies with Oops
deferred()->promise->sync; # after 5 seconds dies with "TIMEOUT\n"
deferred()->promise->sync(10); # after 10 seconds dies with "TIMEOUT\n"
Runs the promise synchronously. Runs new event loop which is finished
after $timeout (default 5) seconds or when the promise gets fulfilled
or rejected.
If the promise gets fulfilled before timeout, returns the values of the promise.
If the promise gets rejected before timeout, dies with the reason of the promise.
Otherwise dies with C<TIMEOUT> string.
=item C<values>
If the promise was fulfilled, returns the values the underlying deferred object was resolved with.
If the promise was not fulfilled (was rejected or it is still pending), returns an empty list.
=item C<value>
The first element from values the underlying deferred object was resolved with.
If the promise was not fulfilled (was rejected or it is still pending), returns undef.
Having
my $d = deferred();
$d->resolve( 'a', 20 );
my $p = $d->promise;
$p->values; # (returns ('a', 20))
$p->value; # (returns 'a')
=item C<reason>
If the promise was rejected, returns the reason the underlying deferred object was resolved with.
If the promise was not rejected (was fulfilled or it is still pending) returns undef.
lib/AnyEvent/Promises.pm view on Meta::CPAN
my $d = deferred();
$d->resolve();
$d->promise->then($arg);
otherwise it is an equivalent to:
my $d = deferred();
$d->resolve($arg);
$d->promise;
=item C<is_promise($arg)>
Returns true if the argument is a promise (object with method C<then>).
=back
=head1 SEE ALSO
=over 4
=item L<AnyEvent>
To use this module it is necessary to have basic understanding of L<AnyEvent> event loop.
=item L<Promises>
Although L<AnyEvent::Promises> is similar to L<Promises>
(and you can use its more thorough documentation to understand the concept of promises)
there are important differences.
AnyEvent::Promises does not work without running event loop
based on L<AnyEvent>. All C<$on_fulfilled>, C<$on_rejected> handlers
(arguments of C<then> method) are run in "next tick" of event loop
as is required in 2.2.4 of the promises spec L<< http://promises-aplus.github.io/promises-spec/#point-39 >>.
There is also a crucial difference in C<$on_reject> handler behaviour
(exception handling). Look at
my $d = deferred();
$d->reject($reason);
my $p = $d->promise->then(
sub { },
sub {
return @_;
}
);
$p->then(
sub {
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>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Roman Daniel.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
( run in 0.777 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )