then(function ($v) { echo $v; }); * * @template TValue = mixed * @template TReason = mixed * * @implements PromiseInterface * * @see https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration */ final class Coroutine implements PromiseInterface { use NonSerializableTrait; /** * @var PromiseInterface|null */ private ?PromiseInterface $currentPromise = null; /** * @var Generator */ private Generator $generator; /** * @var Promise */ private PromiseInterface $result; /** * @param callable(): Generator $generatorFn */ public function __construct(callable $generatorFn) { $this->generator = $generatorFn(); $this->result = new Promise(function (): void { while (isset($this->currentPromise)) { $this->currentPromise->wait(); } }, function (): void { if (isset($this->currentPromise)) { $this->currentPromise->cancel(); } }); try { $this->nextCoroutine($this->generator->current()); } catch (\Throwable $throwable) { $this->result->reject($throwable); } } /** * Create a new coroutine. * * @param callable(): Generator $generatorFn * * @return self */ public static function of(callable $generatorFn): self { return new self($generatorFn); } /** * @template TFulfilledValue = never * @template TFulfilledReason = never * @template TRejectedValue = never * @template TRejectedReason = never * * @param (callable(TValue): (TFulfilledValue|PromiseInterface))|null $onFulfilled Invoked when the promise fulfills. * @param (callable(TReason): (TRejectedValue|PromiseInterface))|null $onRejected Invoked when the promise is rejected. * * @return PromiseInterface<($onFulfilled is null ? TValue : TFulfilledValue)|($onRejected is null ? never : TRejectedValue), ($onFulfilled is null ? never : TFulfilledReason|\Throwable)|($onRejected is null ? TReason : TRejectedReason|\Throwable)> */ public function then( ?callable $onFulfilled = null, ?callable $onRejected = null ): PromiseInterface { return $this->result->then($onFulfilled, $onRejected); } /** * @template TRejectedValue = never * @template TRejectedReason = never * * @param callable(TReason): (TRejectedValue|PromiseInterface) $onRejected Invoked when the promise is rejected. * * @return PromiseInterface */ public function otherwise(callable $onRejected): PromiseInterface { return $this->result->otherwise($onRejected); } public function wait(bool $unwrap = true) { return $this->result->wait($unwrap); } public function getState(): string { return $this->result->getState(); } public function resolve($value = null): void { $this->result->resolve($value); } public function reject($reason): void { $this->result->reject($reason); } public function cancel(): void { if (isset($this->currentPromise)) { $this->currentPromise->cancel(); } $this->result->cancel(); } private function nextCoroutine($yielded): void { $this->currentPromise = Create::promiseFor($yielded) ->then([$this, '_handleSuccess'], [$this, '_handleFailure']); } /** * @internal */ public function _handleSuccess($value): void { $this->currentPromise = null; try { $next = $this->generator->send($value); if ($this->generator->valid()) { $this->nextCoroutine($next); } else { $this->result->resolve($value); } } catch (\Throwable $throwable) { $this->result->reject($throwable); } } /** * @internal */ public function _handleFailure($reason): void { $this->currentPromise = null; try { $nextYield = $this->generator->throw(Create::exceptionFor($reason)); // The throw was caught, so keep iterating on the coroutine $this->nextCoroutine($nextYield); } catch (\Throwable $throwable) { $this->result->reject($throwable); } } }