Ejemplo n.º 1
0
 function testPendingFail()
 {
     $finalValue = 0;
     $promise = new Promise();
     $promise->then(null, function ($value) use(&$finalValue) {
         $finalValue = $value + 2;
     });
     $promise->reject(4);
     Loop\run();
     $this->assertEquals(6, $finalValue);
 }
Ejemplo n.º 2
0
use function Sabre\Event\coroutine;
require __DIR__ . '/../vendor/autoload.php';
/**
 * This example shows demonstrates the Promise api.
 */
/* Creating a new promise */
$promise = new Promise();
/* After 2 seconds we fulfill it */
Loop\setTimeout(function () use($promise) {
    echo "Step 1\n";
    $promise->fulfill("hello");
}, 2);
/* Callback chain */
$result = $promise->then(function ($value) {
    echo "Step 2\n";
    // Immediately returning a new value.
    return $value . " world";
})->then(function ($value) {
    echo "Step 3\n";
    // This 'then' returns a new promise which we resolve later.
    $promise = new Promise();
    // Resolving after 2 seconds
    Loop\setTimeout(function () use($promise, $value) {
        $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();