-
-
Save anonymous/baec1c0dc22909f5af9b 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
diff --git a/lib/Mojolicious/Guides/Cookbook.pod b/lib/Mojolicious/Guides/Cookbook.pod | |
index 9e61ca1..4706b60 100644 | |
--- a/lib/Mojolicious/Guides/Cookbook.pod | |
+++ b/lib/Mojolicious/Guides/Cookbook.pod | |
@@ -424,34 +424,24 @@ L<Mojolicious::Plugin::DefaultHelpers/"delay">, which can help you avoid deep | |
nested closures that often result from continuation-passing style. | |
use Mojolicious::Lite; | |
+ use Mojo::IOloop; | |
use Mojo::URL; | |
# Search MetaCPAN for "mojo" and "minion" | |
get '/' => sub { | |
my $c = shift; | |
- # Prepare response in two steps | |
- $c->delay( | |
- | |
- # Concurrent requests | |
- sub { | |
- my $delay = shift; | |
- my $url = Mojo::URL->new('api.metacpan.org/v0/module/_search'); | |
- $url->query({sort => 'date:desc'}); | |
- $c->ua->get($url->clone->query({q => 'mojo'}) => $delay->begin); | |
- $c->ua->get($url->clone->query({q => 'minion'}) => $delay->begin); | |
- }, | |
- | |
- # Delayed rendering | |
- sub { | |
- my ($delay, $mojo, $minion) = @_; | |
- $c->render(json => { | |
- mojo => $mojo->res->json('/hits/hits/0/_source/release'), | |
- minion => $minion->res->json('/hits/hits/0/_source/release') | |
- }); | |
- } | |
- ); | |
- }; | |
+ my $url = Mojo::URL->new('api.metacpan.org/v0/module/_search'); | |
+ $url->query({sort => 'date:desc'}); | |
+ my $mojo_promise = $c->ua->aget($url->clone->query({q => 'mojo'})); | |
+ my $minion_promise = $c->ua->aget($url->clone->query({q => 'minion'})); | |
+ Mojo::IOLoop->collect($mojo_promise, $minion_promise)->then(sub { | |
+ my ($mojo, $minion) = @_; | |
+ $c->render(json => { | |
+ mojo => $mojo->res->json('/hits/hits/0/_source/release'), | |
+ minion => $minion->res->json('/hits/hits/0/_source/release') | |
+ }); | |
+ }); | |
app->start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment