Async-Defer

 view release on metacpan or  search on metacpan

lib/Async/Defer.pm  view on Meta::CPAN

        my ($d, %taskresults) = @_;
        if (ref $taskresults{task1}) {
            print "task1 results:",  @{ $taskresults{task1} };
        } else {
            print "task1 throw error:", $taskresults{task1};
        }
    });

=head3 if

=head3 else

=head3 end_if

    $defer = $defer->if( \&conditional );
    $defer = $defer->else();
    $defer = $defer->end_if();

Add conditional I<OPERATOR> to this object's I<program>.

When this I<OPERATOR> should be executed, C<\&conditional> will be called
with single param:

    ( $defer_object )

The C<\&conditional> B<MUST> be sync, and return true/false.

=head3 while

=head3 end_while

    $defer = $defer->while( \&conditional );
    $defer = $defer->end_while();

Add loop I<OPERATOR> to this object's I<program>.

When this I<OPERATOR> should be executed, C<\&conditional> will be called with
single param:

    ( $defer_object )

The C<\&conditional> B<MUST> be sync, and return true/false.

=head3 try

=head3 catch

    $defer = $defer->try();
    $defer = $defer->catch(
        $regex_or_FINALLY => \&sync_or_async_code,
        ...
    );

Add exception handling to this object's I<program>.

In general, try/catch/finally behaviour is same as in Java (and probably
many other languages).

If some I<STATEMENTS> inside try/catch block will C<throw()>, the thrown error
can be intercepted (using matching regexp in C<catch()>) and handled in any
way (blocked - if C<catch()> handler call C<done()>, C<continue()> or C<break()> or
replaced by another exception - if C<catch()> handler call C<throw()>).
If exception match more than one regexp, first successfully matched
regexp's handler will be used. Handler will be executed with params:

    ( $defer_object, $error )

In addition to exception handlers you can also define FINALLY handler
(by using string C<"FINALLY"> instead of regex). FINALLY handler will be
called in any case (with/without exception) and may handle this in any way
just like any other exception handler in C<catch()>. FINALLY handler will
be executed with different params:

    # with exception
    ( $defer_object, $error)
    # without exception
    ( $defer_object, @optional_results_from_previous_STATEMENT )

=head2 FLOW CONTROL in STATEMENTS

Unless you are nesting child defers, one and only one of these methods B<MUST> be
called at end of each I<STATEMENT>, both sync and async!
In the case of nested defers, see L</"NESTED DEFERS">.

=head3 done

    $defer->done( @optional_result );

Go to continue I<STATEMENT>/I<OPERATOR>. If continue is I<STATEMENT>, it will receive
C<@optional_result> in it parameters.

=head3 throw

    $defer->throw( $error );

Throw exception. Nearest matching C<catch()> or FINALLY I<STATEMENT> will be
executed and receive C<$error> in it parameter.

=head3 continue

    $defer->continue();

Move to beginning of nearest C<while()> (or to first I<STATEMENT> if
called outside C<while()>) and continue with continue iteration (if C<while()>'s
C<\&conditional> still returns true).

=head3 break

    $defer->break();

Move to first I<STATEMENT>/I<OPERATOR> after nearest C<while()> (or finish this
I<program> if called outside C<while()> - returning to parent's Defer object
if any).


=head1 SUPPORT

=head2 Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker
at L<https://github.com/powerman/perl-Async-Defer/issues>.



( run in 0.480 second using v1.01-cache-2.11-cpan-2398b32b56e )