You are here

private function Promise::settle in Lockr 7.3

2 calls to Promise::settle()
Promise::reject in vendor/guzzlehttp/promises/src/Promise.php
Reject the promise with the given reason.
Promise::resolve in vendor/guzzlehttp/promises/src/Promise.php
Resolve the promise with the given value.

File

vendor/guzzlehttp/promises/src/Promise.php, line 121

Class

Promise
Promises/A+ implementation that avoids recursion when possible.

Namespace

GuzzleHttp\Promise

Code

private function settle($state, $value) {
  if ($this->state !== self::PENDING) {

    // Ignore calls with the same resolution.
    if ($state === $this->state && $value === $this->result) {
      return;
    }
    throw $this->state === $state ? new \LogicException("The promise is already {$state}.") : new \LogicException("Cannot change a {$this->state} promise to {$state}");
  }
  if ($value === $this) {
    throw new \LogicException('Cannot fulfill or reject a promise with itself');
  }

  // Clear out the state of the promise but stash the handlers.
  $this->state = $state;
  $this->result = $value;
  $handlers = $this->handlers;
  $this->handlers = null;
  $this->waitList = $this->waitFn = null;
  $this->cancelFn = null;
  if (!$handlers) {
    return;
  }

  // If the value was not a settled promise or a thenable, then resolve
  // it in the task queue using the correct ID.
  if (!method_exists($value, 'then')) {
    $id = $state === self::FULFILLED ? 1 : 2;

    // It's a success, so resolve the handlers in the queue.
    queue()
      ->add(static function () use ($id, $value, $handlers) {
      foreach ($handlers as $handler) {
        self::callHandler($id, $value, $handler);
      }
    });
  }
  elseif ($value instanceof Promise && $value
    ->getState() === self::PENDING) {

    // We can just merge our handlers onto the next promise.
    $value->handlers = array_merge($value->handlers, $handlers);
  }
  else {

    // Resolve the handlers when the forwarded promise is resolved.
    $value
      ->then(static function ($value) use ($handlers) {
      foreach ($handlers as $handler) {
        self::callHandler(1, $value, $handler);
      }
    }, static function ($reason) use ($handlers) {
      foreach ($handlers as $handler) {
        self::callHandler(2, $reason, $handler);
      }
    });
  }
}