/** * Resolves this promise with the given promise if this promise is still pending. * * @param \Icicle\Promise\PromiseInterface $result */ private function resolve(PromiseInterface $result) { if (null !== $this->result) { return; } $this->result = $result; $this->result->done($this->onFulfilled, $this->onRejected ?: new ThenQueue()); $this->onFulfilled = null; $this->onRejected = null; $this->onCancelled = null; }
/** * {@inheritdoc} */ public function getResult() { return $this->result->getResult(); }
/** * {@inheritdoc} */ public function wait() { return $this->result->wait(); }
/** * This function may be used to synchronously wait for a promise to be resolved. This function should generally * not be used within a running event loop, but rather to set up a task (or set of tasks, then use join() or another * function to group them) and synchronously wait for the task to complete. Using this function in a running event * loop will not block the loop, but it will prevent control from moving past the call to this function and disrupt * program flow. * * @param PromiseInterface $promise * * @return mixed Promise fulfillment value. * * @throws \Icicle\Promise\Exception\UnresolvedError If the event loop empties without fulfilling the promise. * @throws \Throwable If the promise is rejected, the rejection reason is thrown from this function. */ function wait(PromiseInterface $promise) { while ($promise->isPending()) { if (Loop\isEmpty()) { throw new UnresolvedError('Loop emptied without resolving promise.'); } Loop\tick(true); } $result = $promise->getResult(); if ($promise->isRejected()) { throw $result; } return $result; }