From fd14f8c6b665c031a2a51006e95601d0cab3ff65 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 13 May 2019 13:53:31 +0200 Subject: [PATCH] Use custom wait --- src/danog/MadelineProto/Tools.php | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/danog/MadelineProto/Tools.php b/src/danog/MadelineProto/Tools.php index 26c75bbc..812b758b 100644 --- a/src/danog/MadelineProto/Tools.php +++ b/src/danog/MadelineProto/Tools.php @@ -200,16 +200,28 @@ trait Tools } elseif (!($promise instanceof Promise)) { return $promise; } + + $resolved = false; do { try { - return wait($promise); - } catch (\Throwable $e) { - if ($e->getMessage() !== 'Loop stopped without resolving the promise') { - //$this->logger->logger("AN EXCEPTION SURFACED " . $e, \danog\MadelineProto\Logger::ERROR); - throw $e; - } + Loop::run(function () use (&$resolved, &$value, &$exception, $promise) { + $promise->onResolve(function ($e, $v) use (&$resolved, &$value, &$exception) { + Loop::stop(); + $resolved = true; + $exception = $e; + $value = $v; + }); + }); + } catch (\Throwable $throwable) { + throw new \Error("Loop exceptionally stopped without resolving the promise", 0, $throwable); } - } while (true); + } while (!$resolved); + + if ($exception) { + throw $exception; + } + + return $value; } public function all($promises) {