Subject: | Non-nested, one condvar, no fixed-order AnyEvent example |
Using the begin and end (and the cb mutator) methods of the condvar to group callbacks:
use AnyEvent;
use AnyEvent::HTTP;
use strict;
use warnings;
use Data::Dumper;
my $cv = AnyEvent->condvar;
my ( $product, $suggestions, $reviews ) = ( [], [], [] );
$cv->begin;
http_get(
'http://rest.api.example.com/-/product/12345',
sub {
($product) = @_;
$cv->end;
}
);
$cv->begin;
http_get(
'http://rest.api.example.com/-/product/suggestions?for_sku=12345',
sub {
($suggestions) = @_;
$cv->end;
}
);
$cv->begin;
http_get(
'http://rest.api.example.com/-/product/reviews?for_sku=12345',
sub {
($reviews) = @_;
$cv->end;
}
);
$cv->cb(
sub {
$cv->send(
{
product => $product,
suggestions => $suggestions,
reviews => $reviews,
}
);
}
);
my $all_product_info = $cv->recv;
use AnyEvent;
use AnyEvent::HTTP;
use strict;
use warnings;
use Data::Dumper;
my $cv = AnyEvent->condvar;
my ( $product, $suggestions, $reviews ) = ( [], [], [] );
$cv->begin;
http_get(
'http://rest.api.example.com/-/product/12345',
sub {
($product) = @_;
$cv->end;
}
);
$cv->begin;
http_get(
'http://rest.api.example.com/-/product/suggestions?for_sku=12345',
sub {
($suggestions) = @_;
$cv->end;
}
);
$cv->begin;
http_get(
'http://rest.api.example.com/-/product/reviews?for_sku=12345',
sub {
($reviews) = @_;
$cv->end;
}
);
$cv->cb(
sub {
$cv->send(
{
product => $product,
suggestions => $suggestions,
reviews => $reviews,
}
);
}
);
my $all_product_info = $cv->recv;