public function sendAsyncRequest(RequestInterface $request)
    {
        $reactRequest = $this->buildReactRequest($request);
        $promise = new Promise($this->loop);

        $reactRequest->on('error', function (\Exception $error) use ($promise, $request) {
            $promise->reject(new RequestException(
                $error->getMessage(),
                $request,
                $error
            ));
        });

        $reactRequest->on('response', function (ReactResponse $reactResponse = null) use ($promise, $request) {
            $bodyStream = $this->streamFactory->createStream();
            $reactResponse->on('data', function ($data) use (&$bodyStream) {
                $bodyStream->write((string) $data);
            });

            $reactResponse->on('end', function (\Exception $error = null) use ($promise, $request, $reactResponse, &$bodyStream) {
                $response = $this->buildResponse(
                    $reactResponse,
                    $bodyStream
                );
                if (null !== $error) {
                    $promise->reject(new HttpException(
                        $error->getMessage(),
                        $request,
                        $response,
                        $error
                    ));
                } else {
                    $promise->resolve($response);
                }
            });
        });

        $reactRequest->end((string) $request->getBody());

        return $promise;
    }
public function sendAsyncRequest(RequestInterface $request)
    {
        $reactRequest = $this->buildReactRequest($request);
        $promise = new Promise($this->loop);

        $reactRequest->on('error', function (\Exception $error) use ($promise, $request) {
            $promise->reject(new RequestException(
                $error->getMessage(),
                $request,
                $error
            ));
        });

        $reactRequest->on('response', function (ReactResponse $reactResponse = null) use ($promise, $request) {
            $bodyStream = $this->streamFactory->createStream();
            $reactResponse->on('data', function ($data) use (&$bodyStream) {
                $bodyStream->write((string) $data);
            });

            $reactResponse->on('end', function (\Exception $error = null) use ($promise, $request, $reactResponse, &$bodyStream) {
                $response = $this->buildResponse(
                    $reactResponse,
                    $bodyStream
                );
                if (null !== $error) {
                    $promise->reject(new HttpException(
                        $error->getMessage(),
                        $request,
                        $response,
                        $error
                    ));
                } else {
                    $promise->resolve($response);
                }
            });
        });

        $reactRequest->end((string) $request->getBody());

        return $promise;
    }
{@inheritdoc}