Skip to content

Instantly share code, notes, and snippets.

/gather.pl Secret

Created February 3, 2015 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0349879c0fea80bce380 to your computer and use it in GitHub Desktop.
Save anonymous/0349879c0fea80bce380 to your computer and use it in GitHub Desktop.
# 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