*/ final class Coroutine implements Promise { private Promise $promise; /** * @param \Generator $generator * @psalm-param \Generator,mixed,Promise|ReactPromise|TReturn> $generator */ public function __construct(\Generator $generator) { $this->promise = async(static function () use ($generator): mixed { $yielded = $generator->current(); while ($generator->valid()) { try { $yielded = $generator->send(await($yielded)); } catch (\Throwable $exception) { $yielded = $generator->throw($exception); } } return $generator->getReturn(); }); } /** @inheritDoc */ public function onResolve(callable $onResolved): void { $this->promise->onResolve($onResolved); } }