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; }
public function testPromiseTest__PendingAfterFulfilledWaitUnwrapFalse() { $this->object = new Client($this->store); $result = new Client($this->store); $this->object->resolve($result); $result->resolve('result'); $this->assertEquals($this->object->wait(false), 'result'); }
private function checkIfFinished() { if (!$this->pending && !$this->iterable->valid()) { // Resolve the promise if there's nothing left to do. $this->aggregate->resolve(null); return true; } return false; }
/** * 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 (\Exception $e) { $promise->reject($e); } }); return $promise; }
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 (\Exception $e) { $p->reject($e); } } }); return $p; }
public function testPromiseAsMonad() { $result = Promise::resolve(2)->bind(function ($value) { return Promise::resolve(3)->bind(function ($value2) use($value) { return $value + $value2; }); }); $this->assertEquals('Promise(5)', (string) $result); }
public function resolveWith(Promise $context) { $context->resolve(); $this->args = func_get_args() and array_shift($this->args); $this->_execute($this->done, $context); $this->_execute($this->always, $context); }