title: Perl AnyEvent callback on latency sub, how run it async?
body: <p>I start to learn AnyEvent and have some trobles with it.
  I totally misunderstood how its possible to get asynchronous profit, fe :</p>
  
  <pre><code>#!/usr/bin/env perl
  
  package LatencySub;
  
  use strict;
  use warnings;
  use AnyEvent;
  
  # sub for emulate latency - is it right way?
  sub do_delay{
      my ($name, $delay) = (@_);
      my $cv = AE::cv;
      my $timer = AE::timer $delay, 0, sub { $cv-&gt;send() };
      $cv-&gt;recv;
      return $name.' proceed, delay is '.$delay;
  };
  
  
  package main;
  
  use 5.12.0;
  use warnings;
  
  use Smart::Comments;
  
  use AnyEvent;
  
  my @list = (
      { name =&gt; 'first', delay =&gt; 1  },
      { name =&gt; 'second', delay =&gt; 1 },
      { name =&gt; 'third', delay =&gt; 2 }
  );
  
  sub process_cb {
      my ( $name, $delay, $cb ) = @_;
      my $result = LatencySub::do_delay( $name, $delay );
      $cb-&gt;($result);
  }
  
  my %result;
  
  my $cv = AE::cv;
  # outer loop
  $cv-&gt;begin (sub { shift-&gt;send (\%result) });
  
  my $before_time =  AE::time;
  ### foreach start...
  foreach my $entity (@list) {
              $cv-&gt;begin;
              process_cb ( 
                                  $entity-&gt;{'name'},
                                  $entity-&gt;{'delay'},
                                  sub {
                                       $result{$entity-&gt;{'name'}} = shift;
                                       $cv-&gt;end;
                                  }
              );
       }
  ### foreach end...
  
  $cv-&gt;end;
  my $time_all = AE::time - $before_time;
  
  ### $time_all
  ### %result
  </code></pre>
  
  <p>At output I got:</p>
  
  <pre><code>### foreach start...
  
  ### foreach end...
  
  ### $time_all: '4.02105116844177'
  ### %result: {
  ###            first =&gt; 'first proceed, delay is 1',
  ###            second =&gt; 'second proceed, delay is 1',
  ###            third =&gt; 'third proceed, delay is 2'
  ###          }
  </code></pre>
  
  <p>All delay sum (1+1+2) eq $time_all - 4 seconds.
  So, no profit at all.</p>
  
  <p>Why is it and how I can (and is it possible?) create "right" callback?</p>