From 5c1b3c8414de8718d27737c318cdb8a2acdf4eb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 13 May 2018 13:18:11 +0200 Subject: [PATCH] Update blocking example to use all() helper --- examples/11-http-blocking.php | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/examples/11-http-blocking.php b/examples/11-http-blocking.php index 08bd4f5..9c2bfde 100644 --- a/examples/11-http-blocking.php +++ b/examples/11-http-blocking.php @@ -23,23 +23,21 @@ function download(array $urls) $loop = Factory::create(); $browser = new Browser($loop); - $queue = new Queue(3, null, function ($url) use ($browser) { - return $browser->get($url); - }); - - $promises = array(); - foreach ($urls as $url) { - $promises[$url] = $queue($url)->then( - function (ResponseInterface $response) use ($url) { + $urls = array_combine($urls, $urls); + $promise = Queue::all(3, $urls, function ($url) use ($browser) { + return $browser->get($url)->then( + function (ResponseInterface $response) { + // return only the body for successful responses return $response->getBody(); }, - function (Exception $e) use ($url) { + function (Exception $e) { + // return null body for failed requests return null; } ); - } + }); - return Block\awaitAll($promises, $loop); + return Block\await($promise, $loop); } $responses = download($urls);