You are here

public function Promise::then in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/guzzlehttp/promises/src/Promise.php \GuzzleHttp\Promise\Promise::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 Promise::then()
Promise::otherwise in vendor/guzzlehttp/promises/src/Promise.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/Promise.php, line 30

Class

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

Namespace

GuzzleHttp\Promise

Code

public function then(callable $onFulfilled = null, callable $onRejected = null) {
  if ($this->state === self::PENDING) {
    $p = new Promise(null, [
      $this,
      'cancel',
    ]);
    $this->handlers[] = [
      $p,
      $onFulfilled,
      $onRejected,
    ];
    $p->waitList = $this->waitList;
    $p->waitList[] = $this;
    return $p;
  }

  // Return a fulfilled promise and immediately invoke any callbacks.
  if ($this->state === self::FULFILLED) {
    return $onFulfilled ? promise_for($this->result)
      ->then($onFulfilled) : promise_for($this->result);
  }

  // It's either cancelled or rejected, so return a rejected promise
  // and immediately invoke any callbacks.
  $rejection = rejection_for($this->result);
  return $onRejected ? $rejection
    ->then(null, $onRejected) : $rejection;
}