Skip to content

Commit

Permalink
let Mojolicious race MetaCPAN
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 24, 2017
1 parent 1e83833 commit 43a73ac
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -411,15 +411,17 @@ Callbacks will be passed along to L<Mojo::IOLoop::Delay/"steps">.
# Wrap continuation-passing style APIs with promises
my $ua = Mojo::UserAgent->new;
sub aget {
sub get {
my $promise = Mojo::IOLoop->delay;
$ua->get(@_ => sub {
my ($ua, $tx) = @_;
$promise->resolve($tx);
});
return $promise;
}
aget('http://mojolicious.org')->then(sub { say shift->res->code })->wait;
my $mojo = get('http://mojolicious.org');
my $cpan = get('http://metacpan.org');
$mojo->race($cpan)->then(sub { say shift->req->url })->wait;
# Synchronize multiple non-blocking operations
my $delay = Mojo::IOLoop->delay(sub { say 'BOOM!' });
Expand Down
6 changes: 4 additions & 2 deletions lib/Mojo/IOLoop/Delay.pm
Expand Up @@ -185,15 +185,17 @@ Mojo::IOLoop::Delay - Promises/A+ and flow-control helpers
# Wrap continuation-passing style APIs with promises
my $ua = Mojo::UserAgent->new;
sub aget {
sub get {
my $promise = Mojo::IOLoop->delay;
$ua->get(@_ => sub {
my ($ua, $tx) = @_;
$promise->resolve($tx);
});
return $promise;
}
aget('http://mojolicious.org')->then(sub { say shift->res->code })->wait;
my $mojo = get('http://mojolicious.org');
my $cpan = get('http://metacpan.org');
$mojo->race($cpan)->then(sub { say shift->req->url })->wait;
# Synchronize multiple non-blocking operations
my $delay = Mojo::IOLoop::Delay->new;
Expand Down

0 comments on commit 43a73ac

Please sign in to comment.