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;
 }
Exemple #2
0
 public function testPromiseThen__ThenRejectedByPromiseButResolved()
 {
     $result = new Client($this->store);
     $promise1 = new Client($this->store);
     $this->object = $promise1->then(null, function ($reason) {
         return $reason->getMessage() . ' was resolved';
     });
     $promise1->reject($result);
     $this->assertEquals($result->getId() . ' was resolved', $this->object->wait(false));
     $this->assertEquals(PromiseInterface::FULFILLED, $this->object->getState());
 }
Exemple #3
0
 public function testThatStateTransitions()
 {
     $promise = new Promise();
     $this->assertEquals('pending', $promise->getState());
     $promise = new Promise();
     $promise->fulfill(1);
     $this->assertEquals('fulfilled', $promise->getState());
     $promise = new Promise();
     $promise->reject(1);
     $this->assertEquals('rejected', $promise->getState());
 }
 private function step($idx)
 {
     // If the promise was already resolved, then ignore this step.
     if ($this->aggregate->getState() !== PromiseInterface::PENDING) {
         return;
     }
     unset($this->pending[$idx]);
     if (!$this->checkIfFinished()) {
         // Add more pending promises if possible.
         $this->refillPending();
     }
 }
Exemple #5
0
 private function step($idx)
 {
     // If the promise was already resolved, then ignore this step.
     if ($this->aggregate->getState() !== PromiseInterface::PENDING) {
         return;
     }
     unset($this->pending[$idx]);
     // Only refill pending promises if we are not locked, preventing the
     // EachPromise to recursively invoke the provided iterator, which
     // cause a fatal error: "Cannot resume an already running generator"
     if ($this->advanceIterator() && !$this->checkIfFinished()) {
         // Add more pending promises if possible.
         $this->refillPending();
     }
 }
Exemple #6
0
 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;
 }