Async-Util
view release on metacpan or search on metacpan
... # asynchronous subs
],
cb => \&do_this_at_the_end,
);
Examples using AnyEvent:
use AnyEvent;
use Async::Util qw(amap);
my @timers;
my $delayed_double = sub {
my ($input, $cb) = @_;
push @timers, AnyEvent->timer(after => 2, cb => sub {
$cb->($input*2);
});
};
my $cv = AE::cv;
amap(
inputs => [ 1 .. 20 ],
action => $delayed_double,
cb => sub { $cv->send(@_) },
my ($res, $err) = $cv->recv;
# achain
my $cv = AE::cv;
achain(
input => 2,
steps => [
sub {
my ($input, $cb) = @_;
push @timers, AnyEvent->timer(
after => 0,
cb => sub { $cb->($input+1) },
);
},
sub {
my ($input, $cb) = @_;
push @timers, AnyEvent->timer(
after => 0,
cb => sub { $cb->($input * 2) },
);
},
],
cb => sub { $cv->send(@_) },
);
my ($res, $err) = $cv->recv; # $res is 6
lib/Async/Util.pm view on Meta::CPAN
... # asynchronous subs
],
cb => \&do_this_at_the_end,
);
Examples using AnyEvent:
use AnyEvent;
use Async::Util qw(amap);
my @timers;
my $delayed_double = sub {
my ($input, $cb) = @_;
push @timers, AnyEvent->timer(after => 2, cb => sub {
$cb->($input*2);
});
};
my $cv = AE::cv;
amap(
inputs => [ 1 .. 20 ],
action => $delayed_double,
cb => sub { $cv->send(@_) },
lib/Async/Util.pm view on Meta::CPAN
my ($res, $err) = $cv->recv;
# achain
my $cv = AE::cv;
achain(
input => 2,
steps => [
sub {
my ($input, $cb) = @_;
push @timers, AnyEvent->timer(
after => 0,
cb => sub { $cb->($input+1) },
);
},
sub {
my ($input, $cb) = @_;
push @timers, AnyEvent->timer(
after => 0,
cb => sub { $cb->($input * 2) },
);
},
],
cb => sub { $cv->send(@_) },
);
my ($res, $err) = $cv->recv; # $res is 6
);
my ($results, $err) = $cv->recv;
confess $err if $err;
is_deeply $results, [ 2, 6, 12 ], 'outputs look right';
}
{ # achain
my @timers;
my $cv = AE::cv;
achain(
input => 2,
steps => [
sub {
my ($input, $cb) = @_;
push @timers, AnyEvent->timer(
after => 0,
cb => sub { $cb->($input+1) },
);
},
sub {
my ($input, $cb) = @_;
push @timers, AnyEvent->timer(
after => 0,
cb => sub { $cb->($input * 2) },
);
},
],
cb => sub { $cv->send(@_) },
);
my ($res) = $cv->recv;
( run in 0.981 second using v1.01-cache-2.11-cpan-49f99fa48dc )