You are here

public function RejectedPromise::then in Lockr 7.3

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 RejectedPromise::then()
RejectedPromise::otherwise in vendor/guzzlehttp/promises/src/RejectedPromise.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/RejectedPromise.php, line 24

Class

RejectedPromise
A promise that has been rejected.

Namespace

GuzzleHttp\Promise

Code

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