mirror of
https://github.com/danog/amp.git
synced 2024-11-30 04:29:08 +01:00
Fix promise combinator example to use Artax v3
This commit is contained in:
parent
916cf3119d
commit
a976a73cbc
@ -20,21 +20,23 @@ asynchronous operations at the same time. Let's look at a simple example using t
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Amp\Loop;
|
use Amp\Loop;
|
||||||
use function Amp\Promise;
|
use Amp\Promise;
|
||||||
|
|
||||||
Loop::run(function () {
|
Loop::run(function () {
|
||||||
$httpClient = new Amp\Artax\Client;
|
$httpClient = new Amp\Artax\DefaultClient;
|
||||||
$promiseArray = $httpClient->requestMulti([
|
$uris = [
|
||||||
"google" => "http://www.google.com",
|
"google" => "http://www.google.com",
|
||||||
"news" => "http://news.google.com",
|
"news" => "http://news.google.com",
|
||||||
"bing" => "http://www.bing.com",
|
"bing" => "http://www.bing.com",
|
||||||
"yahoo" => "https://www.yahoo.com",
|
"yahoo" => "https://www.yahoo.com",
|
||||||
]);
|
];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// magic combinator sauce to flatten the promise
|
// magic combinator sauce to flatten the promise
|
||||||
// array into a single 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) {
|
foreach ($responses as $key => $response) {
|
||||||
printf(
|
printf(
|
||||||
@ -46,9 +48,8 @@ Loop::run(function () {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (Amp\MultiReasonException $e) {
|
} catch (Amp\MultiReasonException $e) {
|
||||||
// If any one of the requests fails the combo
|
// If any one of the requests fails the combo will fail and
|
||||||
// promise returned by Amp\all() will fail and
|
// be thrown back into our generator.
|
||||||
// be thrown back into our generator here.
|
|
||||||
echo $e->getMessage(), "\n";
|
echo $e->getMessage(), "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user