/** * Wait for the promise to be fulfilled or rejected. * * When this method returns, the request has been resolved and the appropriate callable has terminated. * * When called with the unwrap option * * @param bool $unwrap Whether to return resolved value / throw reason or not * * @return \Psr\Http\Message\ResponseInterface|null Resolved value, null if $unwrap is set to false * * @throws \Http\Client\Exception The rejection reason. */ public function wait($unwrap = true) { $this->runner->wait($this->core); if ($unwrap) { if ($this->core->getState() === self::REJECTED) { throw $this->core->getException(); } return $this->core->getResponse(); } return null; }
/** * Sends a PSR-7 request in an asynchronous way. * * @param RequestInterface $request * * @return Promise * * @throws \Http\Client\Exception\RequestException On invalid request. * @throws \InvalidArgumentException For invalid header names or values. * @throws \RuntimeException If creating the body stream fails. * * @since 1.6 \UnexpectedValueException replaced with RequestException. * @since 1.0 */ public function sendAsyncRequest(RequestInterface $request) { if (!$this->multiRunner instanceof MultiRunner) { $this->multiRunner = new MultiRunner(); } $handle = curl_init(); $responseBuilder = $this->createResponseBuilder(); $options = $this->createCurlOptions($request, $responseBuilder); curl_setopt_array($handle, $options); $core = new PromiseCore($request, $handle, $responseBuilder); $promise = new CurlPromise($core, $this->multiRunner); $this->multiRunner->add($core); return $promise; }