1

I need to get response from async request and retry request if it's not successful. I'm trying to test with printing message in log file if exception is called, but I'm not getting this error message. What I tried so far -> I followed this link: https://github.com/reactphp/promise#how-promise-forwarding-works

        $promise = Api::async()->get($url)->then(
            function ($result) {
                $this->handleResult($result);
            }
        )->otherwise(function (\Exception $x) {
            // Propagate the rejection
            info('error');
            throw $x;
        });
        $promise->wait();
        $promise = Api::async()->get($url)->then(
            function ($result) {
                $this->handleResult($result);
            }
        )->then(function ($result) {
            info('error');
            throw new \Exception($result);
        })->otherwise(function (\Exception $result) {
            // Handle the rejection, and don't propagate.
            // This is like catch without a rethrow
            info('error');
            return $result->getMessage();
        })
        ->then(function ($result) {
            info('error');
        });
            $promise->wait();

These two I tried from this docs: https://docs.php-http.org/en/latest/components/promise.html

        $promise = Api::async()->get($url)->then(
            function ($result) {
                $this->handleResult($result);
            }
        );
        try {
            $promise->wait();
        } catch (\Exception $exception) {
            info('error');
        }
        $promise = Api::async()->get($url)->then(
            // The success callback
            function (ResponseInterface $res) {
            },
            function (\Exception $e) {
                info('error');
            }
        );
        $promise->wait();
justsimpleuser
  • 149
  • 1
  • 14
  • 1
    I wrote answer related to your question here should solve the problem, https://stackoverflow.com/q/56183592/9471283 also this one https://stackoverflow.com/a/66811321/9471283 , If it does not work don't forget to mention here, don't forget to upvote the answer if it worked for you – bhucho Aug 12 '21 at 09:27
  • Thank you for your response, I was able handle response by getting status code and then do action which depends by given status code – justsimpleuser Aug 13 '21 at 12:03

0 Answers0