Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deferred when in a non async context #110

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 10 additions & 0 deletions src/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Http\Client\Common;

use Http\Client\Exception;
use Http\Client\Promise\HttpFulfilledPromise;
use Http\Client\Promise\HttpRejectedPromise;
use Http\Promise\Promise;
use Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -36,6 +38,14 @@ public function __construct(callable $waitCallback)
*/
public function then(callable $onFulfilled = null, callable $onRejected = null)
{
if ($this->state === self::FULFILLED) {
return (new HttpFulfilledPromise($this->value))->then($onFulfilled, $onRejected);
}

if ($this->state === self::REJECTED) {
return (new HttpRejectedPromise($this->failure))->then($onFulfilled, $onRejected);
}

$deferred = new self($this->waitCallback);

$this->onFulfilledCallbacks[] = function (ResponseInterface $response) use ($onFulfilled, $deferred) {
Expand Down