-
-
Save anonymous/0349879c0fea80bce380 to your computer and use it in GitHub Desktop.
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
# Synchronize multiple events | |
my $delay = Mojo::IOLoop->delay(sub { say 'BOOM!' }); | |
for my $i (1 .. 10) { | |
my $cb = $delay->gather; | |
Mojo::IOLoop->timer($i => sub { | |
say 10 - $i; | |
$cb->(); | |
}); | |
} | |
$delay->wait; | |
# Sequentialize multiple events | |
Mojo::IOLoop->delay( | |
# First step (simple timer) | |
sub { | |
my $delay = shift; | |
Mojo::IOLoop->timer(2 => $delay->gather); | |
say 'Second step in 2 seconds.'; | |
}, | |
# Second step (concurrent timers) | |
sub { | |
my $delay = shift; | |
Mojo::IOLoop->timer(1 => $delay->gather); | |
Mojo::IOLoop->timer(3 => $delay->gather); | |
say 'Third step in 3 seconds.'; | |
}, | |
# Third step (the end) | |
sub { say 'And done after 5 seconds total.' } | |
)->wait; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment