Skip to content

Commit dca54bf

Browse files
committed
Call callbacks when already resolved
1 parent c3889f7 commit dca54bf

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/Promise.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
9595
}
9696
};
9797

98+
if ($this->state === HttpPromise::FULFILLED) {
99+
$this->doResolve($this->response);
100+
}
101+
102+
if ($this->state === HttpPromise::REJECTED) {
103+
$this->doReject($this->exception);
104+
}
105+
98106
return $newPromise;
99107
}
100108

@@ -113,6 +121,11 @@ public function resolve(ResponseInterface $response)
113121

114122
$this->state = HttpPromise::FULFILLED;
115123
$this->response = $response;
124+
$this->doResolve($response);
125+
}
126+
127+
private function doResolve(ResponseInterface $response)
128+
{
116129
$onFulfilled = $this->onFulfilled;
117130

118131
if (null !== $onFulfilled) {
@@ -135,6 +148,11 @@ public function reject(Exception $exception)
135148

136149
$this->state = HttpPromise::REJECTED;
137150
$this->exception = $exception;
151+
$this->doReject($exception);
152+
}
153+
154+
private function doReject(Exception $exception)
155+
{
138156
$onRejected = $this->onRejected;
139157

140158
if (null !== $onRejected) {

0 commit comments

Comments
 (0)