HttplugPromise example



    public function sendAsyncRequest(RequestInterface $request): HttplugPromise
    {
        if (!$promisePool = $this->promisePool) {
            throw new \LogicException(sprintf('You cannot use "%s()" as the "guzzlehttp/promises" package is not installed. Try running "composer require guzzlehttp/promises".', __METHOD__));
        }

        try {
            $response = $this->sendPsr7Request($request, true);
        } catch (NetworkException $e) {
            return new HttplugPromise(new RejectedPromise($e));
        }

        $waitLoop = $this->waitLoop;

        $promise = new GuzzlePromise(static function D) use ($response$waitLoop) {
            $waitLoop->wait($response);
        }static function D) use ($response$promisePool) {
            $response->cancel();
            unset($promisePool[$response]);
        });

        
use Symfony\Component\HttpClient\Response\HttplugPromise;

class HttplugPromiseTest extends TestCase
{
    public function testComplexNesting()
    {
        $mkPromise = function D$result): HttplugPromise {
            $guzzlePromise = new Promise(function D) use (&$guzzlePromise$result) {
                $guzzlePromise->resolve($result);
            });

            return new HttplugPromise($guzzlePromise);
        };

        $promise1 = $mkPromise('result');
        $promise2 = $promise1->then($mkPromise);
        $promise3 = $promise2->then(fn ($result) => $result);

        $this->assertSame('result', $promise3->wait());
    }
}
Home | Imprint | This part of the site doesn't use cookies.