current(); $r = function ($res) use ($gen, &$r, &$f, $resolve, $reject) { $gen->send($res); if (!$gen->valid()) { $ret = $gen->getReturn(); $resolve($ret); } $newProm = $gen->current(); return $newProm->then($r, $f); }; $f = function ($reason) use ($gen, &$r, &$f, $resolve, $reject) { try { $toThrow = $reason instanceof Throwable ? $reason : new Exception($reason); $gen->throw($toThrow); if (!$gen->valid()) { return $resolve($gen->getReturn()); } $newProm = $gen->current(); return $newProm->then($r, $f); } catch (Exception $e) { return $reject($e); } }; $promise->then($r, $f); }); }