Skip to content

Instantly share code, notes, and snippets.

@mishin
Forked from dolmen/parallel-fetch.pl
Created September 5, 2013 12:48
Show Gist options
  • Save mishin/6449658 to your computer and use it in GitHub Desktop.
Save mishin/6449658 to your computer and use it in GitHub Desktop.
# Alternate implementation of https://metacpan.org/module/STEVAN/Promises-0.03/lib/Promises/Cookbook/TIMTOWTDI.pod
# that can run the requests in parallel
# See also a more concrete application of this: https://gist.github.com/dolmen/6306377
my $all_product_info = {};
my $cv = AnyEvent->condvar;
$cv->begin;
http_get('http://rest.api.example.com/-/product/12345', sub {
my ($product) = @_;
$all_product_info->{product} = $product;
$cv->end;
});
$cv->begin;
http_get('http://rest.api.example.com/-/product/suggestions?for_sku=12345', sub {
my ($suggestions) = @_;
$all_product_info->{suggestions} = $suggestions;
$cv->end;
});
$cv->begin;
http_get('http://rest.api.example.com/-/product/reviews?for_sku=12345', sub {
my ($reviews) = @_;
$all_product_info->{reviews} = $reviews;
$cv->end;
}),
$cv->recv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment