public function then(callable $onFulfilled = null, callable $onRejected = null) { // If there's no onRejected callback then just return self. if (!$onRejected) { return $this; } $queue = queue(); $reason = $this->reason; $p = new Promise([$queue, 'run']); $queue->add(static function () use($p, $reason, $onRejected) { if ($p->getState() === self::PENDING) { try { // Return a resolved promise if onRejected does not throw. $p->resolve($onRejected($reason)); } catch (\Throwable $e) { // onRejected threw, so return a rejected promise. $p->reject($e); } catch (\Exception $e) { // onRejected threw, so return a rejected promise. $p->reject($e); } } }); return $p; }
/** * Adds a function to run in the task queue when it is next `run()` and returns * a promise that is fulfilled or rejected with the result. * * @param callable $task Task function to run. * * @return PromiseInterface */ function task(callable $task) { $queue = queue(); $promise = new Promise([$queue, 'run']); $queue->add(function () use($task, $promise) { try { $promise->resolve($task()); } catch (\Throwable $e) { $promise->reject($e); } catch (\Exception $e) { $promise->reject($e); } }); return $promise; }
private function addPending() { if (!$this->iterable || !$this->iterable->valid()) { return false; } $promise = promise_for($this->iterable->current()); $idx = $this->iterable->key(); try { $this->iterable->next(); } catch (\Exception $e) { $this->aggregate->reject($e); return false; } $this->pending[$idx] = $promise->then(function ($value) use($idx) { if ($this->onFulfilled) { call_user_func($this->onFulfilled, $value, $idx, $this->aggregate); } $this->step($idx); }, function ($reason) use($idx) { if ($this->onRejected) { call_user_func($this->onRejected, $reason, $idx, $this->aggregate); } $this->step($idx); }); return true; }
public function testPromiseTest__PendingAfterRejecteddWaitUnwrapFalse() { $this->object = new Client($this->store); $result = new Client($this->store); $this->object->reject($result); $this->assertInstanceOf('zaboy\\async\\Promise\\Exception\\ReasonPendingException', $this->object->wait(false)); $this->assertTrue($result->isId($this->object->wait(false)->getMessage())); }
private function advanceIterator() { try { $this->iterable->next(); return true; } catch (\Exception $e) { $this->aggregate->reject($e); return false; } }
public function then(callable $onFulfilled = null, callable $onRejected = null) { // Return itself if there is no onFulfilled function. if (!$onFulfilled) { return $this; } $queue = queue(); $p = new Promise([$queue, 'run']); $value = $this->value; $queue->add(static function () use($p, $value, $onFulfilled) { if ($p->getState() === self::PENDING) { try { $p->resolve($onFulfilled($value)); } catch (\Throwable $e) { $p->reject($e); } catch (\Exception $e) { $p->reject($e); } } }); return $p; }
private function advanceIterator() { // Place a lock on the iterator so that we ensure to not recurse, // preventing fatal generator errors. if ($this->mutex) { return false; } $this->mutex = true; try { $this->iterable->next(); $this->mutex = false; return true; } catch (\Exception $e) { $this->aggregate->reject($e); $this->mutex = false; return false; } }
function testAllReject() { $promise1 = new Promise(); $promise2 = new Promise(); $finalValue = 0; Promise::all([$promise1, $promise2])->then(function ($value) use(&$finalValue) { $finalValue = 'foo'; return 'test'; }, function ($value) use(&$finalValue) { $finalValue = $value; }); $promise1->reject(1); $this->assertEquals(1, $finalValue); $promise2->reject(2); $this->assertEquals(1, $finalValue); }
function testDeepException() { $start = 0; $promise = new Promise(); coroutine(function () use(&$start, $promise) { $start += 1; $start += (yield $promise); })->otherwise(function ($e) use(&$start) { $start += $e->getMessage(); }); $this->assertEquals(1, $start); $promise->reject(new \Exception(2)); Loop\run(); $this->assertEquals(3, $start); }
function testWaitRejectedNonScalar() { $promise = new Promise(); Loop\nextTick(function () use($promise) { $promise->reject([]); }); try { $promise->wait(); $this->fail('We did not get the expected exception'); } catch (\Exception $e) { $this->assertInstanceOf('Exception', $e); $this->assertEquals('Promise was rejected with reason of type: array', $e->getMessage()); } }
/** * @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); } } }); }
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); }
public function rejectWith(Promise $context) { $context->reject(); $this->args = func_get_args() and array_shift($this->args); $this->_execute($this->fail, $context); $this->_execute($this->always, $context); }