.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Future::AsyncAwait::Awaitable 3pm" .TH Future::AsyncAwait::Awaitable 3pm 2024-03-07 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME "Future::AsyncAwait::Awaitable" \- the interface required by "Future::AsyncAwait" .SH DESCRIPTION .IX Header "DESCRIPTION" This module documents the method interface required by \f(CW\*(C`Future::AsyncAwait\*(C'\fR to operate on future instances returned by expressions invoked by the \f(CW\*(C`await\*(C'\fR keyword, and returned by functions declared by \f(CW\*(C`async sub\*(C'\fR. This information is largely of relevance to implementors of other module integrations, event systems, or similar. It is not necessary to make regular use of the syntax provided by the module when working with existing event systems. .PP The methods required by this interface are all capitalised and prefixed with \&\f(CW\*(C`AWAIT_...\*(C'\fR, ensuring they are unlikely to clash with existing methods on a class which may have differing semantics. .SS Role::Tiny .IX Subsection "Role::Tiny" If Role::Tiny is available, this module declares itself to be a role that requires the following named methods. The role supplies no code to the applied class, but can be useful for checking that you have in fact implemented all of the required methods. .SS "Conformance Test" .IX Subsection "Conformance Test" To assist implementors of alternative future-like classes, an API conformance test suite is provided by Test::Future::AsyncAwait::Awaitable. You may find this useful to check that your implementation is suitable. .SH CONSTRUCTORS .IX Header "CONSTRUCTORS" The following methods are expected to create new future instances. They make use of the class set by the prevailing \f(CW\*(C`future_class\*(C'\fR import argument, if set, or default to \f(CW\*(C`Future\*(C'\fR if not. .SS AWAIT_NEW_DONE .IX Subsection "AWAIT_NEW_DONE" Generate a new immediate future that is successful. The future will already be ready and have the list of values set as its result. .PP .Vb 1 \& $f = $CLASS\->AWAIT_NEW_DONE( @results ) \& \& # $f\->AWAIT_IS_READY will be true \& # $f\->AWAIT_GET will return @results .Ve .SS AWAIT_NEW_FAIL .IX Subsection "AWAIT_NEW_FAIL" Generate a new immediate future that is failed. The future will already be ready and invoking the "AWAIT_GET" method will throw the given exception. .PP .Vb 1 \& $f = $CLASS\->AWAIT_NEW_FAIL( $message ) \& \& # $f\->AWAIT_IS_READY will be true \& # $f\->AWAIT_GET will throw $message .Ve .SH "INSTANCE METHODS" .IX Header "INSTANCE METHODS" .SS AWAIT_CLONE .IX Subsection "AWAIT_CLONE" Generate a new pending future of the same type as an existing one, which is not modified by doing so. It will only be invoked on instances that are currently pending. .PP .Vb 1 \& $new_f = $f\->AWAIT_CLONE .Ve .PP If the instance has any fields that are required for successful operation (such as application-wide context or event system components) these ought to be copied. The method should not otherwise copy any per-instance state such as pending callbacks or partial results. .SS AWAIT_DONE .IX Subsection "AWAIT_DONE" Sets the success result of an existing still-pending future. It will only be invoked on future instances that are currently pending. .PP .Vb 1 \& $f\->AWAIT_DONE( @results ) \& \& # $f\->AWAIT_IS_READY will now be true \& # $f\->AWAIT_GET will now return @results .Ve .SS AWAIT_FAIL .IX Subsection "AWAIT_FAIL" Sets the failure result of an existing still-pending future. It will only be invoked on future instances that are currently pending. .PP .Vb 1 \& $f\->AWAIT_FAIL( $message ) \& \& # $f\->AWAIT_IS_READY will now be true \& # $f\->AWAIT_GET will now throw $message .Ve .SS AWAIT_IS_READY .IX Subsection "AWAIT_IS_READY" Returns true if a future is ready (successful, failed or cancelled); false if still pending. .PP .Vb 1 \& $bool = $f\->AWAIT_IS_READY .Ve .SS AWAIT_IS_CANCELLED .IX Subsection "AWAIT_IS_CANCELLED" Returns true is a future has already been cancelled; false if still pending, successful or failed. .PP .Vb 1 \& $bool = $f\->AWAIT_IS_CANCELLED .Ve .PP An implementation that does not support cancellation can simply return a constant false here: .PP .Vb 1 \& sub AWAIT_IS_CANCELLED { 0 } .Ve .SS AWAIT_GET .IX Subsection "AWAIT_GET" Yields the result of a successful future (or just the first value if called in scalar context). Throws the failure message as an exception if called on a a failed one. Will not be invoked on a pending or cancelled future. .PP .Vb 3 \& @result = $f\->AWAIT_GET \& $result = $f\->AWAIT_GET \& $f\->AWAIT_GET .Ve .SS AWAIT_ON_READY .IX Subsection "AWAIT_ON_READY" Attach a new CODE reference to be invoked when the future becomes ready (by success or failure). The arguments and context that \f(CW$code\fR is invoked with are unspecified. .PP .Vb 1 \& $f\->AWAIT_ON_READY( $code ) .Ve .SS AWAIT_CHAIN_CANCEL .IX Subsection "AWAIT_CHAIN_CANCEL" Attach a future instance to be cancelled when another one is cancelled. .PP .Vb 1 \& $f1\->AWAIT_CHAIN_CANCEL( $f2 ) .Ve .PP When \f(CW$f1\fR is cancelled, then \f(CW$f2\fR is cancelled. There is no link from \&\f(CW$f2\fR back to \f(CW$f1\fR \- whenever \f(CW$f2\fR changes state here, nothing special happens to \f(CW$f1\fR. .PP An implementation that does not support cancellation can simply ignore this method. .PP .Vb 1 \& sub AWAIT_CHAIN_CANCEL { } .Ve .PP An older version of this API specification named this \f(CW\*(C`AWAIT_ON_CANCEL\*(C'\fR, but that name will be repurposed for attaching code blocks in a later version. .SS AWAIT_ON_CANCEL .IX Subsection "AWAIT_ON_CANCEL" Attach a new CODE reference to be invoked when the future is cancelled. .PP .Vb 1 \& $f\->AWAIT_ON_CANCEL( $code ) .Ve .PP An implementation that does not support cancellation can simply ignore this method. .PP .Vb 1 \& sub AWAIT_ON_CANCEL { } .Ve .SS AWAIT_WAIT .IX Subsection "AWAIT_WAIT" Called by the toplevel \f(CW\*(C`await\*(C'\fR expression in order to run the event system and wait for the instance to be ready. It should return results or throw an exception in the same manner as "AWAIT_GET". .PP .Vb 3 \& @result = $f\->AWAIT_WAIT \& $result = $f\->AWAIT_WAIT \& $f\->AWAIT_WAIT .Ve .SH AUTHOR .IX Header "AUTHOR" Paul Evans