You are here

class FulfilledPromise in Lockr 7.3

A promise that has been fulfilled.

Thenning off of this promise will invoke the onFulfilled callback immediately and ignore other callbacks.

Hierarchy

Expanded class hierarchy of FulfilledPromise

2 files declare their use of FulfilledPromise
CurlFactory.php in vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php
StreamHandler.php in vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php

File

vendor/guzzlehttp/promises/src/FulfilledPromise.php, line 10

Namespace

GuzzleHttp\Promise
View source
class FulfilledPromise implements PromiseInterface {
  private $value;
  public function __construct($value) {
    if (method_exists($value, 'then')) {
      throw new \InvalidArgumentException('You cannot create a FulfilledPromise with a promise.');
    }
    $this->value = $value;
  }
  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;
  }
  public function otherwise(callable $onRejected) {
    return $this
      ->then(null, $onRejected);
  }
  public function wait($unwrap = true, $defaultDelivery = null) {
    return $unwrap ? $this->value : null;
  }
  public function getState() {
    return self::FULFILLED;
  }
  public function resolve($value) {
    if ($value !== $this->value) {
      throw new \LogicException("Cannot resolve a fulfilled promise");
    }
  }
  public function reject($reason) {
    throw new \LogicException("Cannot reject a fulfilled promise");
  }
  public function cancel() {

    // pass
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FulfilledPromise::$value private property
FulfilledPromise::cancel public function Cancels the promise if possible. Overrides PromiseInterface::cancel
FulfilledPromise::getState public function Get the state of the promise ("pending", "rejected", or "fulfilled"). Overrides PromiseInterface::getState
FulfilledPromise::otherwise public function Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of the callback if it is called, or to its original fulfillment value if the promise is instead fulfilled. Overrides PromiseInterface::otherwise
FulfilledPromise::reject public function Reject the promise with the given reason. Overrides PromiseInterface::reject
FulfilledPromise::resolve public function Resolve the promise with the given value. Overrides PromiseInterface::resolve
FulfilledPromise::then public function Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler. Overrides PromiseInterface::then
FulfilledPromise::wait public function Waits until the promise completes if possible. Overrides PromiseInterface::wait
FulfilledPromise::__construct public function
PromiseInterface::FULFILLED constant
PromiseInterface::PENDING constant
PromiseInterface::REJECTED constant