Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Symfony/Component/HttpClient/Response/HttplugPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpClient\Response;

use function GuzzleHttp\Promise\promise_for;
use GuzzleHttp\Promise\PromiseInterface as GuzzlePromiseInterface;
use Http\Promise\Promise as HttplugPromiseInterface;
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
Expand All @@ -31,7 +32,10 @@ public function __construct(GuzzlePromiseInterface $promise)

public function then(callable $onFulfilled = null, callable $onRejected = null): self
{
return new self($this->promise->then($onFulfilled, $onRejected));
return new self($this->promise->then(
$this->wrapThenCallback($onFulfilled),
$this->wrapThenCallback($onRejected)
));
}

public function cancel(): void
Expand Down Expand Up @@ -62,4 +66,15 @@ public function wait($unwrap = true)

return $result;
}

private function wrapThenCallback(?callable $callback): ?callable
{
if (null === $callback) {
return null;
}

return static function ($value) use ($callback) {
return promise_for($callback($value));
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpClient\Tests\Response;

use GuzzleHttp\Promise\Promise;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Response\HttplugPromise;

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

return new HttplugPromise($guzzlePromise);
};

$promise1 = $mkPromise('result');
$promise2 = $promise1->then($mkPromise);
$promise3 = $promise2->then(function ($result) { return $result; });

$this->assertSame('result', $promise3->wait());
}
}