/** * {@inheritdoc} */ public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onNotify = null) { if ($this->state()->equals(State::PENDING())) { $resolver = new ThenResolver($onFulfilled, $onRejected, $onNotify); $this->resolvers[] = $resolver; return $resolver->promise(); } return $this->actual->then($onFulfilled, $onRejected, $onNotify); }
/** * @param PromiseInterface|ExtendedPromiseInterface|mixed $promiseOrValue * * @return FulfilledPromise|Promise */ function resolve($promiseOrValue = null) { if (false === $promiseOrValue instanceof PromiseInterface) { return new FulfilledPromise($promiseOrValue); } if ($promiseOrValue instanceof ExtendedPromiseInterface) { return $promiseOrValue; } return new Promise(function ($resolve, $reject, $notify) use($promiseOrValue) { $promiseOrValue->then($resolve, $reject, $notify); }); }
/** * Promise handler * * @param array|PromiseInterface $promise * @param Generator $gen */ private function give($promise, Generator $gen) { if ($promise instanceof PromiseInterface) { $promise->then(function ($result) use($gen) { if ($promise = $gen->send($result)) { $this->give($promise, $gen); } }); } else { all($promise)->then(function ($results) use($gen) { if ($promise = $gen->send($results)) { $this->give($promise, $gen); } }); } $this->client->send(); }