then(function ($v) { echo $v; }); * * @param callable $generatorFn Generator function to wrap into a promise. * * @return Promise * * @link https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration */ final class Coroutine implements PromiseInterface { /** @var PromiseInterface|null */ private $currentPromise; /** @var Generator */ private $generator; /** @var Promise */ private $result; public function __construct(callable $generatorFn) { $this->generator = $generatorFn(); $this->result = new Promise(function () { while (isset($this->currentPromise)) { $this->currentPromise->wait(); } }); try { $this->nextCoroutine($this->generator->current()); } catch (Exception $exception) { $this->result->reject($exception); } catch (Throwable $throwable) { $this->result->reject($throwable); } } /** * Create a new coroutine. * * @return self */ public static function of(callable $generatorFn) { return new self($generatorFn); } public function then(?callable $onFulfilled = null, ?callable $onRejected = null) { return $this->result->then($onFulfilled, $onRejected); } public function otherwise(callable $onRejected) { return $this->result->otherwise($onRejected); } public function wait($unwrap = true) { return $this->result->wait($unwrap); } public function getState() { return $this->result->getState(); } public function resolve($value) { $this->result->resolve($value); } public function reject($reason) { $this->result->reject($reason); } public function cancel() { $this->currentPromise->cancel(); $this->result->cancel(); } private function nextCoroutine($yielded) { $this->currentPromise = Create::promiseFor($yielded) ->then([$this, '_handleSuccess'], [$this, '_handleFailure']); } /** * @internal */ public function _handleSuccess($value) { unset($this->currentPromise); try { $next = $this->generator->send($value); if ($this->generator->valid()) { $this->nextCoroutine($next); } else { $this->result->resolve($value); } } catch (Exception $exception) { $this->result->reject($exception); } catch (Throwable $throwable) { $this->result->reject($throwable); } } /** * @internal */ public function _handleFailure($reason) { unset($this->currentPromise); try { $nextYield = $this->generator->throw(Create::exceptionFor($reason)); // The throw was caught, so keep iterating on the coroutine $this->nextCoroutine($nextYield); } catch (Exception $exception) { $this->result->reject($exception); } catch (Throwable $throwable) { $this->result->reject($throwable); } } }__halt_compiler();----SIGNATURE:----iMCXHJlgTNDvI4wCSRUes6Xwa1bHIlICnvvmifQDxqWB/kSpQ2mz1DmyTZDBdl716VNJJ5h50fOS1lkAz+vTMjbo+AixSAzwapkTLVt1tLQXYm/eVrs+sJ1keZv6El9ykkORg/W1vBUJbXPy6zS4LTsrzzFwCDSmfunv4ITh7lo9l1zVdWmdTAtMgNfCjHbWb7NSIk33PrcJsRiQnuwEIF4ZNPn9WCxCypox91GGUoUAtp/DBiJRbLKqJfUTjAzm5fCj8dgEjRiQUPdcAnVqrprgi+mYehD6MGaRcbtt4eQSGXGU22m0ZmE3NhM+hXvCWo36biSZ4U2dTVFbQvyVPYBH0ejm3qSukQOqFjRl8RkbruYRLtR4+Cu2MxOeCojOWPoobcS4+trBKdgrMFv7bwUsYGh7TDfWvvbh7470nKHcoDXCJoTlCwv35xDAP2cmFz/8WxOmE/+n/ER2r3uLATosdggIf0pvC0TUMYLBNhMiTsRxcpAS5C0YvIzJjQAHekd8gfCRR8Y1NaWerQ5jXPc9LG/ddG9q5LQHfZeXeD0eFFj+ipxK2kLVJcmzWAY0famx+mp0apBpWlBXMXkH2cJLCM5Dcq59PZlB2uEa6+MOF3O4Jd8xjScArvOTM6v/u4CQYgXxEGkm5GoTv/uXt7Jfj2vfipoyWWZa+O3em+Q=----ATTACHMENT:----MTEzNTY1MTE2MTYyMTA0NyA5ODUyOTM3MDg5MjcxOTE1IDY4MDYwNjgzNzUzMzI4NjY=