diff --git a/examples/6-parallel-requests.php b/examples/6-parallel-requests.php new file mode 100644 index 00000000..6142e353 --- /dev/null +++ b/examples/6-parallel-requests.php @@ -0,0 +1,42 @@ +request($uri); + + return $response->getBody(); + }; + + try { + $promises = []; + + foreach ($uris as $uri) { + $promises[$uri] = Amp\call($requestHandler, $uri); + } + + $bodies = yield $promises; + + foreach ($bodies as $uri => $body) { + print $uri . " - " . \strlen($body) . " bytes" . PHP_EOL; + } + } catch (Amp\Artax\HttpException $error) { + // If something goes wrong Amp will throw the exception where the promise was yielded. + // The Client::request() method itself will never throw directly, but returns a promise. + echo $error; + } +});