Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Promise wait alternatives

Cees-Jan Kiewiet edited this page Apr 12, 2019 · 1 revision

With the latest release of the ReactPHP's event loop it became incompatible with Guzzle's wait mechanism on their promises. For now a hack has been included in this package that should keep it possible, but that is deprecated and in time will throw an exception. Below is a a don't and do example of what not to do and what to do instead:

Don't

Use the sync methods while using this handler:

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use React\EventLoop\Factory;
use WyriHaximus\React\GuzzlePsr7\HttpClientAdapter;

$loop = Factory::create();

echo (new Client([
    'handler' => HandlerStack::create(new HttpClientAdapter($loop)),
]))->get('http://blog.wyrihaximus.net/')->getBody()->getContents(), PHP_EOL;

Or call the wait method on a Guzzle promise.

Do

Use the async methods and wrap the promise in a ReactPHP promise first and then run it through the await function:

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use React\EventLoop\Factory;
use function React\Promise\resolve;
use WyriHaximus\React\GuzzlePsr7\HttpClientAdapter;
use function Clue\React\Block\await;

$loop = Factory::create();

echo await(resolve((new Client([
    'handler' => HandlerStack::create(new HttpClientAdapter($loop)),
]))->getAsync('http://blog.wyrihaximus.net/')), $loop)->getBody()->getContents(), PHP_EOL;
Clone this wiki locally