<?php

$func = function () {
    $result = (yield adder(1, 2));
    $result = (yield adder($result, 3));
    $result = (yield adder($result, 4));
    echo $result;
};
coroutine($func)->wait();
<?php

$func = function () {
    (yield Promise\all($promise1, $promise2));
};
coroutine($func);
Beispiel #3
0
 private function registerErrorHandler()
 {
     set_error_handler(coroutine(function ($errno, $msg, $file, $line) {
         if (!(error_reporting() & $errno)) {
             return;
         }
         $msg = "{$msg} in {$file} on line {$line}";
         switch ($errno) {
             case E_ERROR:
             case E_PARSE:
             case E_USER_ERROR:
             case E_CORE_ERROR:
             case E_COMPILE_ERROR:
             case E_RECOVERABLE_ERROR:
                 $this->logger->error($msg);
                 break;
             case E_CORE_WARNING:
             case E_COMPILE_WARNING:
             case E_WARNING:
             case E_USER_WARNING:
                 $this->logger->warning($msg);
                 break;
             case E_NOTICE:
             case E_USER_NOTICE:
             case E_DEPRECATED:
             case E_USER_DEPRECATED:
             case E_STRICT:
                 $this->logger->notice($msg);
                 break;
             default:
                 $this->logger->warning($msg);
                 break;
         }
     }));
 }
 function testResolveToLastYieldPromise()
 {
     $ok = false;
     $promise = new Promise();
     coroutine(function () use($promise) {
         (yield 'fail');
         (yield $promise);
         $hello = 'hi';
     })->then(function ($value) use(&$ok) {
         $ok = $value;
         $this->fail($reason);
     });
     $promise->fulfill('omg it worked');
     Loop\run();
     $this->assertEquals('omg it worked', $ok);
 }
Beispiel #5
0
/**
 * Concatenates the given observables into a single observable, emitting values from a single observable at a time. The
 * prior observable must complete before values are emitted from any subsequent observable. Observables are concatenated
 * in the order given (iteration order of the array).
 *
 * @param array $observables
 *
 * @return \Amp\Observable
 */
function concat(array $observables) : Observable
{
    foreach ($observables as $observable) {
        if (!$observable instanceof Observable) {
            throw new \Error("Non-observable provided");
        }
    }
    $postponed = new Postponed();
    $subscriptions = [];
    $previous = [];
    $promise = all($previous);
    foreach ($observables as $observable) {
        $subscriptions[] = $observable->subscribe(coroutine(function ($value) use($postponed, $promise) {
            try {
                (yield $promise);
            } catch (\Throwable $exception) {
                // Ignore exception in this context.
            }
            return (yield $postponed->emit($value));
        }));
        $previous[] = $observable;
        $promise = all($previous);
    }
    $promise->when(function ($exception, array $values) use($postponed) {
        if ($exception) {
            $postponed->fail($exception);
            return;
        }
        $postponed->resolve($values);
    });
    return $postponed->observe();
}