Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. RejectedPromise.php

class RejectedPromise

Same name in this branch
  1. 11.1.x vendor/php-http/promise/src/RejectedPromise.php \Http\Promise\RejectedPromise
  2. 11.1.x vendor/guzzlehttp/promises/src/RejectedPromise.php \GuzzleHttp\Promise\RejectedPromise

@internal

@template-implements PromiseInterface<never>

Hierarchy

  • class \React\Promise\Internal\RejectedPromise implements \React\Promise\PromiseInterface

Expanded class hierarchy of RejectedPromise

2 files declare their use of RejectedPromise
functions.php in vendor/react/promise/src/functions.php
Promise.php in vendor/react/promise/src/Promise.php

File

vendor/react/promise/src/Internal/RejectedPromise.php, line 15

Namespace

React\Promise\Internal
View source
final class RejectedPromise implements PromiseInterface {
    
    /** @var \Throwable */
    private $reason;
    
    /** @var bool */
    private $handled = false;
    
    /**
     * @param \Throwable $reason
     */
    public function __construct(\Throwable $reason) {
        $this->reason = $reason;
    }
    
    /** @throws void */
    public function __destruct() {
        if ($this->handled) {
            return;
        }
        $handler = set_rejection_handler(null);
        if ($handler === null) {
            $message = 'Unhandled promise rejection with ' . $this->reason;
            \error_log($message);
            return;
        }
        try {
            $handler($this->reason);
        } catch (\Throwable $e) {
            \preg_match('/^([^:\\s]++)(.*+)$/sm', (string) $e, $match);
            \assert(isset($match[1], $match[2]));
            $message = 'Fatal error: Uncaught ' . $match[1] . ' from unhandled promise rejection handler' . $match[2];
            \error_log($message);
            exit(255);
        }
    }
    
    /**
     * @template TRejected
     * @param ?callable $onFulfilled
     * @param ?(callable(\Throwable): (PromiseInterface<TRejected>|TRejected)) $onRejected
     * @return PromiseInterface<($onRejected is null ? never : TRejected)>
     */
    public function then(?callable $onFulfilled = null, ?callable $onRejected = null) : PromiseInterface {
        if (null === $onRejected) {
            return $this;
        }
        $this->handled = true;
        try {
            return resolve($onRejected($this->reason));
        } catch (\Throwable $exception) {
            return new RejectedPromise($exception);
        }
    }
    
    /**
     * @template TThrowable of \Throwable
     * @template TRejected
     * @param callable(TThrowable): (PromiseInterface<TRejected>|TRejected) $onRejected
     * @return PromiseInterface<TRejected>
     */
    public function catch(callable $onRejected) : PromiseInterface {
        if (!_checkTypehint($onRejected, $this->reason)) {
            return $this;
        }
        
        /**
         * @var callable(\Throwable):(PromiseInterface<TRejected>|TRejected) $onRejected
         */
        return $this->then(null, $onRejected);
    }
    public function finally(callable $onFulfilledOrRejected) : PromiseInterface {
        return $this->then(null, function (\Throwable $reason) use ($onFulfilledOrRejected) : PromiseInterface {
            return resolve($onFulfilledOrRejected())->then(function () use ($reason) : PromiseInterface {
                return new RejectedPromise($reason);
            });
        });
    }
    public function cancel() : void {
        $this->handled = true;
    }
    
    /**
     * @deprecated 3.0.0 Use `catch()` instead
     * @see self::catch()
     */
    public function otherwise(callable $onRejected) : PromiseInterface {
        return $this->catch($onRejected);
    }
    
    /**
     * @deprecated 3.0.0 Use `always()` instead
     * @see self::always()
     */
    public function always(callable $onFulfilledOrRejected) : PromiseInterface {
        return $this->finally($onFulfilledOrRejected);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title
RejectedPromise::$handled private property @var bool
RejectedPromise::$reason private property @var \Throwable
RejectedPromise::always Deprecated public function Overrides PromiseInterface::always
RejectedPromise::cancel public function The `cancel()` method notifies the creator of the promise that there is no
further interest in the results of the operation.
Overrides PromiseInterface::cancel
RejectedPromise::catch public function @template TThrowable of \Throwable
@template TRejected
Overrides PromiseInterface::catch
RejectedPromise::finally public function Allows you to execute &quot;cleanup&quot; type tasks in a promise chain. Overrides PromiseInterface::finally
RejectedPromise::otherwise Deprecated public function Overrides PromiseInterface::otherwise
RejectedPromise::then public function @template TRejected Overrides PromiseInterface::then
RejectedPromise::__construct public function
RejectedPromise::__destruct public function
RSS feed
Powered by Drupal