private function createPromise() { $this->aggregate = new Promise(function () { reset($this->pending); if (empty($this->pending) && !$this->iterable->valid()) { $this->aggregate->resolve(null); return; } // Consume a potentially fluctuating list of promises while // ensuring that indexes are maintained (precluding array_shift). while ($promise = current($this->pending)) { next($this->pending); $promise->wait(); if ($this->aggregate->getState() !== PromiseInterface::PENDING) { return; } } }); // Clear the references when the promise is resolved. $clearFn = function () { $this->iterable = $this->concurrency = $this->pending = null; $this->onFulfilled = $this->onRejected = null; }; $this->aggregate->then($clearFn, $clearFn); }
/** @test */ public function shouldRejectIfResolverThrowsException() { $exception = new \Exception('foo'); $promise = new Promise(function () use($exception) { throw $exception; }); $mock = $this->createCallableMock(); $mock->expects($this->once())->method('__invoke')->with($this->identicalTo($exception)); $promise->then($this->expectCallableNever(), $mock); }
function testPendingFail() { $finalValue = 0; $promise = new Promise(); $promise->then(null, function ($value) use(&$finalValue) { $finalValue = $value + 2; }); $promise->reject(4); $this->assertEquals(6, $finalValue); }
* @param Closure $closure */ public function __construct(Closure $closure) { $this->closure = $closure; $this->start(); } public function run() { $this->synchronized(function () { $closure = $this->closure; $this->result = $closure(); $this->notify(); }); } public function then(callable $callback) { return $this->synchronized(function () use($callback) { if (!$this->result) { $this->wait(); } $callback($this->result); }); } } $promise = new Promise(function () { return file_get_contents('http://google.fr'); }); $promise->then(function ($results) { echo $results; });
public function testThatThenCanBeCalledAfterAPromiseIsRejected() { $promise = new Promise(); $called = 0; $promise->then(function ($value) { throw new \Exception($value); }); $promise->reject(2); $promise->then(null, function () use(&$called) { $called++; }); $this->assertEquals(1, $called); }