48 private $currentPromise;
62 $this->generator = $generatorFn();
63 $this->result =
new Promise(
function () {
64 while (isset($this->currentPromise)) {
65 $this->currentPromise->wait();
68 $this->nextCoroutine($this->generator->current());
72 callable $onFulfilled =
null,
73 callable $onRejected =
null
75 return $this->result->then($onFulfilled, $onRejected);
78 public function otherwise(callable $onRejected)
80 return $this->result->otherwise($onRejected);
83 public function wait($unwrap =
true)
85 return $this->result->wait($unwrap);
90 return $this->result->getState();
95 $this->result->resolve($value);
98 public function reject($reason)
100 $this->result->reject($reason);
105 $this->currentPromise->cancel();
106 $this->result->cancel();
109 private function nextCoroutine($yielded)
112 ->then([$this,
'_handleSuccess'], [$this,
'_handleFailure']);
120 unset($this->currentPromise);
122 $next = $this->generator->send($value);
123 if ($this->generator->valid()) {
124 $this->nextCoroutine($next);
126 $this->result->resolve($value);
128 }
catch (Exception $exception) {
129 $this->result->reject($exception);
130 }
catch (Throwable $throwable) {
131 $this->result->reject($throwable);
140 unset($this->currentPromise);
142 $nextYield = $this->generator->throw(
exception_for($reason));
144 $this->nextCoroutine($nextYield);
145 }
catch (Exception $exception) {
146 $this->result->reject($exception);
147 }
catch (Throwable $throwable) {
148 $this->result->reject($throwable);