<?php

use function Sabre\Event\coroutine;
use Sabre\Event\Promise;
require __DIR__ . '/vendor/autoload.php';
function adder($a, $b)
{
    return new Promise(function ($resolve) use($a, $b) {
        $resolve($a + $b);
    });
}
coroutine(function () {
    try {
        $result = (yield adder(1, 2));
        $result = (yield adder($result, 3));
        $result = (yield adder($result, 4));
        echo $result;
    } catch (Exception $e) {
        echo $e->getMessage();
    }
})->wait();
Exemple #2
0
        $promise->fulfill($value . ", how are ya?");
    }, 2);
    return $promise;
})->then(function ($value) {
    echo "Step 4\n";
    // This is the final event handler.
    return $value . " you rock!";
})->wait();
echo $result, "\n";
/* Now an identical example, this time with coroutines. */
$result = coroutine(function () {
    $promise = new Promise();
    /* After 2 seconds we fulfill it */
    Loop\setTimeout(function () use($promise) {
        echo "Step 1\n";
        $promise->fulfill("hello");
    }, 2);
    $value = (yield $promise);
    echo "Step 2\n";
    $value .= ' world';
    echo "Step 3\n";
    $promise = new Promise();
    Loop\setTimeout(function () use($promise, $value) {
        $promise->fulfill($value . ", how are ya?");
    }, 2);
    $value = (yield $promise);
    echo "Step 4\n";
    // This is the final event handler.
    (yield $value . " you rock!");
})->wait();
echo $result, "\n";
<?php

use Sabre\Event\Promise;
use function Sabre\Event\coroutine;
coroutine(function () {
    (yield asyncOp());
    (yield asyncOp2());
});