src/functions.php
/**
 * Run a coroutine inside a fresh event loop
 *
 * Shortcut to create an event loop with default settings, that suits for most cases.
 *
 * @param callable $coroutine
 *
 * @return mixed
 */
Run a coroutine inside a fresh event loop
Shortcut to create an event loop with default settings, that suits for most cases.

@param callable $coroutine

@return mixed
Run a coroutine inside a fresh event loop Shortcut to create an event loop with default settings that suits for most cases . @param callable $coroutine @return mixed
function run_coroutine_fn(callable $coroutine)
{
    $loop = Factory::create();
    $coroutineFn = function () use ($loop, $coroutine) {
        return $coroutine($loop);
    };
    \GuzzleHttp\Promise\queue(new ReactTaskQueue($loop));
    $globalResult = null;
    $globalError = null;
    $loop->futureTick(function () use ($coroutineFn, &$globalResult, &$globalError) {
        $coroutineInvocation = coroutine($coroutineFn)
            ->then(function ($result) use (&$globalResult) {
                return $globalResult = $result;
            })
            ->otherwise(function ($reason) use (&$globalError) {
                $globalError = \GuzzleHttp\Promise\exception_for($reason);
                return \GuzzleHttp\Promise\rejection_for($reason);
            })
        ;
    });
    $loop->run();
    if ($globalError) {
        throw $globalError;
    }
    return $globalResult;
}
function run_coroutine_fn ( callable $ coroutine ) { $ loop = Factory :: create ( ) ; $ coroutineFn = function ( ) use ( $ loop , $ coroutine ) { return $ coroutine ( $ loop ) ; } ; \ GuzzleHttp \ Promise \ queue ( new ReactTaskQueue ( $ loop ) ) ; $ globalResult = null ; $ globalError = null ; $ loop -> futureTick ( function ( ) use ( $ coroutineFn , & $ globalResult , & $ globalError ) { $ coroutineInvocation = coroutine ( $ coroutineFn ) -> then ( function ( $ result ) use ( & $ globalResult ) { return $ globalResult = $ result ; } ) -> otherwise ( function ( $ reason ) use ( & $ globalError ) { $ globalError = \ GuzzleHttp \ Promise \ exception_for ( $ reason ) ; return \ GuzzleHttp \ Promise \ rejection_for ( $ reason ) ; } ) ; } ) ; $ loop -> run ( ) ; if ( $ globalError ) { throw $ globalError ; } return $ globalResult ; }
Run a coroutine inside a fresh event loop
 Shortcut to create an event loop with default settings, that suits for most cases.
Run a coroutine inside a fresh event loop Shortcut to create an event loop with default settings that suits for most cases .
/** @var \Exception $globalError */ // Reject it again, don't change the state. // Here we are, waiting for the coroutine (promise) to complete. // And check whether there is an exception or not... // TODO Support it.