protected function fail(\Throwable $error): void
    {
        if (~$this->state & AbstractPromise::PENDING) {
            return;
        }
        
        $callbacks = $this->result;
        
        $this->state ^= AbstractPromise::PENDING | AbstractPromise::FAILED;
        $this->result = $error;
        
        if ($this instanceof Cancellable) {
            $this->context->unregisterCancellation($this);
        }
        
        if ($callbacks) {
            if (\is_array($callbacks)) {
                foreach ($callbacks as $callback) {
                    try {
                        $callback($error);
                    } catch (\Throwable $e) {
                        $this->context->handleError($e);
                    }
                }
            } else {
                try {
                    $callbacks($error);
                } catch (\Throwable $e) {
                    $this->context->handleError($e);
                }
            }
        }
    }