I want to use Guzzle to send HTTP requests to multiple endpoints and I want to use the response that comes in first, rather than waiting for all requests to complete.
My code:
$client = new \GuzzleHttp\Client();
$p1 = $client->requestAsync('GET', 'slow.host');
$p2 = $client->requestAsync('GET', 'fast.host');
$any = \GuzzleHttp\Promise\any([$p1, $p2]);
$response = $any->wait();
I was expecting that as soon as either of the promises ($p1, $p2) gets resolved, I would get a response, however that's not how it works with Guzzle. Guzzle will always wait for $p1
to either resolve or reject, even if it takes forever.
From the example above, if the slow.host takes 10 seconds to send a response, and the fast.host takes 1 second to send a response, I would have to wait 10 seconds anyway. I would get the response from fast.host only in case slow.host fails completely (promise gets rejected, no such host, etc).
How do I get the fastest response immediately and ignore the rest?