Exemple #1
0
/**
 * Runs the event loop until the promise is resolved. Should not be called within a running event loop.
 *
 * @param \Interop\Async\Promise $promise
 *
 * @return mixed Promise success value.
 *
 * @throws \Throwable Promise failure reason.
 */
function wait(Promise $promise)
{
    $resolved = false;
    Loop::execute(function () use(&$resolved, &$value, &$exception, $promise) {
        $promise->when(function ($e, $v) use(&$resolved, &$value, &$exception) {
            Loop::stop();
            $resolved = true;
            $exception = $e;
            $value = $v;
        });
    }, Loop::get());
    if (!$resolved) {
        throw new \Error("Loop emptied without resolving promise");
    }
    if ($exception) {
        throw $exception;
    }
    return $value;
}