public function testCanResolveBeforeConsumingAll() { $called = 0; $a = new Promise(function () use(&$a) { $a->resolve('a'); }); $b = new Promise(function () { $this->fail(); }); $each = new EachPromise([$a, $b], ['fulfilled' => function ($value, $idx, Promise $aggregate) use(&$called) { $this->assertSame($idx, 0); $this->assertEquals('a', $value); $aggregate->resolve(null); $called++; }, 'rejected' => function (\Exception $reason) { $this->fail($reason->getMessage()); }]); $p = $each->promise(); $p->wait(); $this->assertNull($p->wait()); $this->assertEquals(1, $called); $this->assertEquals(PromiseInterface::FULFILLED, $a->getState()); $this->assertEquals(PromiseInterface::PENDING, $b->getState()); // Resolving $b has no effect on the aggregate promise. $b->resolve('foo'); $this->assertEquals(1, $called); }
public function run(callable $callback, $time) { $promise = new Promise(); $loop = Runner::getLoop(); $loop->addTimer($time, function () use($promise, $callback) { if ($promise->getState() === PromiseInterface::PENDING) { try { $promise->resolve($callback()); } catch (\Exception $e) { $promise->reject($e); } } }); return $promise; }
public function start() { $promise = new Promise([$this, 'wait']); $this->process->start(function ($type, $buffer) use($promise) { if (Process::ERR === $type) { $this->logger->error($buffer); } else { $this->logger->debug($buffer); } if ($promise->getState() === Promise::PENDING) { if (Process::ERR === $type) { $promise->reject(false); } else { $promise->resolve(true); } } }); return $promise; }
public function testRejectsPromiseWhenCancelFails() { $called = false; $p = new Promise(null, function () use(&$called) { $called = true; throw new \Exception('e'); }); $p->cancel(); $this->assertEquals('rejected', $p->getState()); $this->assertTrue($called); try { $p->wait(); $this->fail(); } catch (\Exception $e) { $this->assertEquals('e', $e->getMessage()); } }
public function getState() { return $this->result->getState(); }