1
0
mirror of https://github.com/danog/amp.git synced 2024-11-26 20:15:00 +01:00

Fix promise combinator example to use Artax v3

This commit is contained in:
Niklas Keller 2017-07-12 23:46:33 +02:00
parent 916cf3119d
commit a976a73cbc

View File

@ -20,21 +20,23 @@ asynchronous operations at the same time. Let's look at a simple example using t
<?php
use Amp\Loop;
use function Amp\Promise;
use Amp\Promise;
Loop::run(function () {
$httpClient = new Amp\Artax\Client;
$promiseArray = $httpClient->requestMulti([
"google" => "http://www.google.com",
"news" => "http://news.google.com",
"bing" => "http://www.bing.com",
"yahoo" => "https://www.yahoo.com",
]);
$httpClient = new Amp\Artax\DefaultClient;
$uris = [
"google" => "http://www.google.com",
"news" => "http://news.google.com",
"bing" => "http://www.bing.com",
"yahoo" => "https://www.yahoo.com",
];
try {
// magic combinator sauce to flatten the promise
// array into a single promise
$responses = yield Promise\all($promiseArray);
$responses = yield array_map(function ($uri) use ($httpClient) {
return $httpClient->request($uri);
}, $uris);
foreach ($responses as $key => $response) {
printf(
@ -46,9 +48,8 @@ Loop::run(function () {
);
}
} catch (Amp\MultiReasonException $e) {
// If any one of the requests fails the combo
// promise returned by Amp\all() will fail and
// be thrown back into our generator here.
// If any one of the requests fails the combo will fail and
// be thrown back into our generator.
echo $e->getMessage(), "\n";
}