public function sendAsyncRequest()

in src/Transport.php [448:495]


    public function sendAsyncRequest(RequestInterface $request): Promise
    {
        $client = $this->getAsyncClient();
        $node = null;
        if (empty($request->getUri()->getHost())) {
            $node = $this->nodePool->nextNode();
            $request = $this->setupConnectionUri($node, $request);
        }
        $request = $this->decorateRequest($request);
        $this->lastRequest = $request;
        $this->logRequest("Async Request", $request);

        $count = 0;
        $promise = $client->sendAsyncRequest($request);

        // onFulfilled callable
        $onFulfilled = function (ResponseInterface $response) use (&$count) {
            $this->lastResponse = $response;
            $this->logResponse("Async Response", $response, $count);
            return $this->getAsyncOnSuccess()->success($response, $count);
        };

        // onRejected callable
        $onRejected = function (Exception $e) use ($client, $request, &$count, $node) {
            $this->logger->error(sprintf("Retry %d: %s", $count, $e->getMessage()));
            $this->getAsyncOnFailure()->failure($e, $request, $count, $node ?? null);
            if (isset($node)) {
                $node->markAlive(false);
                $node = $this->nodePool->nextNode();
                $request = $this->setupConnectionUri($node, $request);
            }
            $count++;
            return $client->sendAsyncRequest($request);
        };
        
        // Add getRetries() callables using then()
        for ($i=0; $i < $this->getRetries(); $i++) {
            $promise = $promise->then($onFulfilled, $onRejected);
        }
        // Add the last getRetries()+1 callable for managing the exceeded error
        $promise = $promise->then($onFulfilled, function(Exception $e) use (&$count) {
            $exceededMsg = sprintf("Exceeded maximum number of retries (%d)", $this->getRetries());
            $this->logger->error(sprintf("Retry %d: %s", $count, $e->getMessage()));
            $this->logger->error($exceededMsg);
            throw new NoNodeAvailableException(sprintf("%s: %s", $exceededMsg, $e->getMessage()));
        });
        return $promise;
    }