Skip to content

Instantly share code, notes, and snippets.

@Logioniz
Last active August 29, 2015 14:08
Show Gist options
  • Save Logioniz/5d05b0c65ca2a4ed07d1 to your computer and use it in GitHub Desktop.
Save Logioniz/5d05b0c65ca2a4ed07d1 to your computer and use it in GitHub Desktop.
Nice example. Why die?)
#!/usr/bin/perl -wl
# code for answer on question
use Mojo::IOLoop;
sub inc {
my ($num, $cb) = @_;
return Mojo::IOLoop->next_tick(sub {$cb->('error')}) unless $num =~ /^\d+$/;
Mojo::IOLoop->timer(0.01 => sub { $cb->(++$num) });
}
my $d = Mojo::IOLoop->delay(sub {
shift;
print @_;
print 'OK';
Mojo::IOLoop->stop;
});
my @args = (1, 5, 'qq', 18);
my @res;
for my $data (@args) {
my $end = $d->begin(0);
inc($data, sub {
my $response = shift;
#print "$data => $response";
push @res, $response;
$end->("$data => $response");
});
}
die "Oops" if 0 != @res;
Mojo::IOLoop->start;
#!/usr/bin/perl -wl
# code for question: Why die?
use Mojo::IOLoop;
sub inc {
my ($num, $cb) = @_;
return $cb->('error') unless $num =~ /^\d+$/;
Mojo::IOLoop->timer(0.01 => sub { $cb->(++$num) });
}
my $d = Mojo::IOLoop->delay(sub {
print 'OK';
Mojo::IOLoop->stop;
});
my @args = (1, 5, 'qq', 18);
my @res;
for my $data (@args) {
my $end = $d->begin;
inc($data, sub {
my $response = shift;
print "$data => $response";
push @res, $response;
$end->();
});
}
die "Oops" if 0 != @res;
Mojo::IOLoop->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment