public function testThatThenCanBeCalledAfterAPromiseIsFulfilled() { $promise = new Promise(); $called = 0; $promise->then(function ($value) { throw new \Exception($value); }); $promise->fulfill(2); $promise->then(function () use(&$called) { $called++; }); $this->assertEquals(1, $called); }
function testAll() { $promise1 = new Promise(); $promise2 = new Promise(); $finalValue = 0; Promise::all([$promise1, $promise2])->then(function ($value) use(&$finalValue) { $finalValue = $value; }); $promise1->fulfill(1); $this->assertEquals(0, $finalValue); $promise2->fulfill(2); $this->assertEquals([1, 2], $finalValue); }
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); }
function testWaitResolve() { $promise = new Promise(); Loop\nextTick(function () use($promise) { $promise->fulfill(1); }); $this->assertEquals(1, $promise->wait()); }
/** * @param Promise $subPromise * @param callable $callBack * @return void */ private function invokeCallback(Promise $subPromise, callable $callBack = null) { nextTick(function () use($callBack, $subPromise) { if (is_callable($callBack)) { try { $result = $callBack($this->value); if ($result instanceof self) { $result->then([$subPromise, 'fulfill'], [$subPromise, 'reject']); } else { $subPromise->fulfill($result); } } catch (\Exception $e) { // If the event handler threw an exception, we need to make sure that // the chained promise is rejected as well. $subPromise->reject($e); } } else { if ($this->state === self::FULFILLED) { $subPromise->fulfill($this->value); } else { $subPromise->reject($this->value); } } }); }