You are here

public function FulfilledPromise::then in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/guzzlehttp/promises/src/FulfilledPromise.php \GuzzleHttp\Promise\FulfilledPromise::then()

Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler.

Parameters

callable $onFulfilled Invoked when the promise fulfills.:

callable $onRejected Invoked when the promise is rejected.:

Return value

PromiseInterface

Overrides PromiseInterface::then

1 call to FulfilledPromise::then()
FulfilledPromise::otherwise in vendor/guzzlehttp/promises/src/FulfilledPromise.php
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.

File

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

Class

FulfilledPromise
A promise that has been fulfilled.

Namespace

GuzzleHttp\Promise

Code

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;
}