/**
  * @return bool
  */
 public function cancel()
 {
     if ($this->promise()->state()->equals(State::PENDING())) {
         $this->reject(new CancellationException());
         return true;
     }
     return false;
 }
Exemple #2
0
 /**
  * @param mixed $result
  * @param bool  $success
  *
  * @throws \LogicException
  */
 private function changeState($result, $success)
 {
     if ($this->state()->equals(State::PENDING())) {
         $this->actual = $success ? new FulfilledPromise($result) : new RejectedPromise($result);
         while (!empty($this->resolvers)) {
             /** @var \Cubiche\Core\Async\Promise\ResolverInterface $resolver */
             $resolver = array_shift($this->resolvers);
             if ($success) {
                 $resolver->resolve($result);
             } else {
                 $resolver->reject($result);
             }
         }
     } else {
         throw new \LogicException(\sprintf('A %s promise cannot be %s', $this->state(), $success ? 'resolved' : 'rejected'));
     }
 }