Skip to content

Instantly share code, notes, and snippets.

@renatocron
Created October 31, 2019 14:07
Show Gist options
  • Save renatocron/c06854ccbd358edafaacf9f10c9bdb1b to your computer and use it in GitHub Desktop.
Save renatocron/c06854ccbd358edafaacf9f10c9bdb1b to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use common::sense;
use DDP;
use Mojo::Promise;
use Mojo::IOLoop::Subprocess;
my $subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->run(
sub {
my $subprocess = shift;
use DDP;
my $new_ioloop = Mojo::IOLoop->new;
my @promises;
for (1 .. 10) {
my $p = Mojo::Promise->new()->ioloop($new_ioloop);
$p->then(
sub {
print STDERR "p is running\n\n";
$new_ioloop->timer(
0.1 => sub {
$p->resolve('Lucky!');
}
)
}
)->catch(sub { use DDP; p \@_; });
push @promises, $p;
}
print STDERR "new_ioloop->race\n";
my $fastest = Mojo::Promise->new()->ioloop($new_ioloop)->race(@promises)->wait();
print STDERR "not today!\n";
return $fastest;
},
sub {
use DDP;
p \@_;
}
);
$subprocess->ioloop->start unless $subprocess->ioloop->is_running;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment