Skip Menu |

This queue is for tickets about the Promises CPAN distribution.

Report information
The Basics
Id: 83992
Status: open
Priority: 0/
Queue: Promises

People
Owner: Nobody in particular
Requestors: james2vegas [...] aim.com
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 0.03
Fixed in: (no value)



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;


Subject: Re: [rt.cpan.org #83992] Non-nested, one condvar, no fixed-order AnyEvent example
Date: Wed, 20 Mar 2013 08:44:31 -0400
To: bug-Promises [...] rt.cpan.org
From: Stevan Little <stevan.little [...] iinteractive.com>
James, Thanks for the example, I have added it into the docs. Personally I believe that the Promises version is simpler and easier to understand, but aside from that the only thing about your example I am not a fan of is the semi-global variables. Thanks, - Stevan On Mar 16, 2013, at 3:23 PM, James Wright via RT <bug-Promises@rt.cpan.org> wrote: Show quoted text
> Sat Mar 16 15:23:15 2013: Request 83992 was acted upon. > Transaction: Ticket created by JWRIGHT > Queue: Promises > Subject: Non-nested, one condvar, no fixed-order AnyEvent example > Broken in: 0.03 > Severity: Wishlist > Owner: Nobody > Requestors: james2vegas@aim.com > Status: new > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=83992 > > > > 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; >
As an alternative to using $product, $suggestions, $reviews you could subclass AnyEvent::CondVar and provide accessors for those fields (or any fields, generically), or (mis)use the fact that, underneath, a Condvar is just a blessed hashref and set $cv->{key}