public function wait($unwrap = true) { if ($unwrap) { return new PromiseException('Do not try to call wait(true)'); } $result = $this->unserializeResult($this->data[Store::RESULT]); if ($result instanceof PromiseInterface && $result->getState() === self::PENDING) { /* @var $result PendingPromise */ //result is pending $reason = $result->getId(); return new ReasonPendingException($reason); } if (!$result instanceof PromiseInterface) { return $result; } $result = parent::wait(false); if (is_a($result, '\\zaboy\\async\\Promise\\Exception\\RejectedException', true)) { //result is exception $reason = 'Exception was thrown while Reason was resolving'; return new ReasonRejectedException($reason, 0, $result); } set_error_handler(function ($number, $string) { throw new PromiseException("RejectedPromise. String: {$string}, Number: {$number}", null, null); }); try { //result can be converted to string return new RejectedException(strval($result)); } catch (\Exception $exc) { //result can not be converted to string $reason = 'Reason cannot be converted to string.'; return new RejectedException($reason, 0, $exc); } restore_error_handler(); }
/** * * @param Store $store * @throws PromiseException */ public function __construct($promiseData = [], $result = null) { parent::__construct($promiseData); $this->data[Store::STATE] = PromiseInterface::FULFILLED; if (!isset($this->data[Store::RESULT]) && !is_null($result)) { $this->data[Store::RESULT] = $this->serializeResult($result); } }