1

I am using this telegram api to make an api call. It works, but i cant get this result returned. This is what I have:

$loop = \React\EventLoop\Factory::create();
$handler = new HttpClientRequestHandler($loop);
$tgLog = new TgLog($token, $handler);

$webHookInfo = new GetWebhookInfo();
$promise = $tgLog->performApiRequest($webHookInfo);
$promiseResult = $promise->then(
  function (WebhookInfo $info)
  {
    print 'Y';
    return $info;
  },
  function (\Exception $e) {
    print 'X';
    return $e->getMessage();
  }
);
$loop->run();

It prints the "Y" and I can even var_dump $info and see the result I want. But outside of that function, I cant access it. This is what I see if I var_dump($promiseResult):

object(React\Promise\Promise)#1582 (6) {
  ["canceller":"React\Promise\Promise":private]=>
  NULL
  ["result":"React\Promise\Promise":private]=>
  object(React\Promise\FulfilledPromise)#1597 (1) {
    ["value":"React\Promise\FulfilledPromise":private]=>
    object(unreal4u\TelegramAPI\Telegram\Types\WebhookInfo)#1450 (8) {
      ["url"]=>          string(84) "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
      ["has_custom_certificate"]=>          bool(false)
      ["pending_update_count"]=>          int(0)
      ["last_error_date"]=>          int(1595489318)
      ["last_error_message"]=>          string(20) "Read timeout expired"
      ["max_connections"]=>          int(100)
      ["allowed_updates"]=>          array(0) {
      }
      ["logger":protected]=>          object(unreal4u\Dummy\Logger)#1047 (0) {
      }
    }
  }
  ["handlers":"React\Promise\Promise":private]=>      array(0) {
  }
  ["progressHandlers":"React\Promise\Promise":private]=>      array(0) {
  }
  ["requiredCancelRequests":"React\Promise\Promise":private]=>      int(0)
  ["cancelRequests":"React\Promise\Promise":private]=>      int(0)
}

I am actually after that url that I blanked out with X's. But there is no method on this object to get that value.

What am I missing?

rockstardev
  • 13,479
  • 39
  • 164
  • 296
  • 2
    You can't access the value outside the function because it does not exists. Since Promises are asynchronous operation, their value only exists inside the scope of the function passed to the `then` function. This, however, does not prevent you from returning the promise and use it somewhere else. You could check out [this answer](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call?rq=1) for more informations. Please note that the answer is in Javascript, but you might be able to use some concept here. – Nicolas Aug 17 '20 at 11:55
  • Yes, I understand. What I want to know is how does one then get the result. What is the correct way to do it? I know it's asynchronous, but I want to get the result synchronously. – rockstardev Aug 17 '20 at 12:37
  • If you really want to get the result synchronously, there might be an options in the library you are using, just like Jquery's ajax has one. But if they made it asynchronous in the first place, there is a good chance it's for a good reason. I would suggest trying to work with the asynchronous nature of AJAX request rather than try to make them synchronous. – Nicolas Aug 17 '20 at 12:48
  • I am just trying to get it to give me a result. I read this is the "new guzzle". Which made me think it will be better at simply executing an API call. It's great that it can do it asynchronously, but I need the result before I can proceed. And I dont know how to get it. It seems the result is there after $loop->run(); but there is no way of getting it out of any object. Im just stuck... – rockstardev Aug 17 '20 at 12:56
  • You get the result from the callback function passed to the "then". If you want to use it somewhere else, then whatever code needs to run next must either be within that function, or in another function which is called from that function (to ensure it doesn't run before it's ready). You can't use a value before it exists, that's the only rule you need to remember here. (And you can't logically `return` from the callback function, because the value would be returned to whatever code inside the Promise triggered it, which isn't interested in it). – ADyson Aug 17 '20 at 13:00
  • 1
    @coderama Your processing will be done inside the callback. Either by calling another function with the values or directly in there. This is the magic of callback, once you get one, everything that need to be executed after that need to be asynchronous aswell. – Nicolas Aug 17 '20 at 13:13

0 Answers0