16 private $handlers = [];
23 callable $waitFn =
null,
24 callable $cancelFn =
null
26 $this->waitFn = $waitFn;
27 $this->cancelFn = $cancelFn;
31 callable $onFulfilled =
null,
32 callable $onRejected =
null
34 if ($this->state === self::PENDING) {
35 $p =
new Promise(
null, [$this,
'cancel']);
36 $this->handlers[] = [$p, $onFulfilled, $onRejected];
37 $p->waitList = $this->waitList;
38 $p->waitList[] = $this;
43 if ($this->state === self::FULFILLED) {
52 return $onRejected ? $rejection->then(
null, $onRejected) : $rejection;
57 return $this->
then(
null, $onRejected);
60 public function wait($unwrap =
true)
62 $this->waitIfPending();
65 ? $this->result->
wait($unwrap)
70 || $this->state === self::FULFILLED
87 if ($this->state !== self::PENDING) {
91 $this->waitFn = $this->waitList =
null;
93 if ($this->cancelFn) {
94 $fn = $this->cancelFn;
95 $this->cancelFn =
null;
98 }
catch (\Throwable $e) {
100 }
catch (\Exception $e) {
106 if ($this->state === self::PENDING) {
113 $this->settle(self::FULFILLED, $value);
118 $this->settle(self::REJECTED, $reason);
121 private function settle($state, $value)
123 if ($this->state !== self::PENDING) {
125 if ($state === $this->state && $value === $this->result) {
128 throw $this->state === $state
129 ? new \LogicException(
"The promise is already {$state}.")
130 : new \LogicException(
"Cannot change a {$this->state} promise to {$state}");
133 if ($value === $this) {
134 throw new \LogicException(
'Cannot fulfill or reject a promise with itself');
138 $this->state = $state;
139 $this->result = $value;
140 $handlers = $this->handlers;
141 $this->handlers =
null;
142 $this->waitList = $this->waitFn =
null;
143 $this->cancelFn =
null;
151 if (!method_exists($value,
'then')) {
152 $id = $state === self::FULFILLED ? 1 : 2;
154 queue()->add(
static function () use ($id, $value, $handlers) {
155 foreach ($handlers as $handler) {
156 self::callHandler($id, $value, $handler);
159 } elseif ($value instanceof Promise
160 && $value->getState() === self::PENDING
163 $value->handlers = array_merge($value->handlers, $handlers);
167 static function ($value) use ($handlers) {
168 foreach ($handlers as $handler) {
169 self::callHandler(1, $value, $handler);
172 static function ($reason) use ($handlers) {
173 foreach ($handlers as $handler) {
174 self::callHandler(2, $reason, $handler);
190 private static function callHandler($index, $value, array $handler)
193 $promise = $handler[0];
197 if ($promise->getState() !== self::PENDING) {
202 if (isset($handler[$index])) {
203 $promise->resolve($handler[$index]($value));
204 } elseif ($index === 1) {
206 $promise->resolve($value);
209 $promise->reject($value);
211 }
catch (\Throwable $reason) {
212 $promise->reject($reason);
213 }
catch (\Exception $reason) {
214 $promise->reject($reason);
218 private function waitIfPending()
220 if ($this->state !== self::PENDING) {
222 } elseif ($this->waitFn) {
223 $this->invokeWaitFn();
224 } elseif ($this->waitList) {
225 $this->invokeWaitList();
228 $this->
reject(
'Cannot wait on a promise that has '
229 .
'no internal wait function. You must provide a wait '
230 .
'function when constructing the promise to be able to '
231 .
'wait on a promise.');
236 if ($this->state === self::PENDING) {
237 $this->
reject(
'Invoking the wait callback did not resolve the promise');
241 private function invokeWaitFn()
244 $wfn = $this->waitFn;
245 $this->waitFn =
null;
247 }
catch (\Exception $reason) {
248 if ($this->state === self::PENDING) {
260 private function invokeWaitList()
262 $waitList = $this->waitList;
263 $this->waitList =
null;
265 foreach ($waitList as $result) {
267 $result->waitIfPending();
269 if ($result->result instanceof Promise) {
270 $result = $result->result;
272 if ($result->result instanceof PromiseInterface) {
273 $result->result->wait(
false);