Last active
August 29, 2015 14:08
-
-
Save Logioniz/5d05b0c65ca2a4ed07d1 to your computer and use it in GitHub Desktop.
Nice example. Why die?)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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