protected function resolve($value = null): void
    {
        if (~$this->state & AbstractPromise::PENDING) {
            return;
        }
        
        if ($value instanceof Promise) {
            $value->when(function (?\Throwable $e, $v = null): void {
                if ($e === null) {
                    $this->resolve($v);
                } else {
                    $this->fail($e);
                }
            });
            
            return;
        }
        
        $callbacks = $this->result;
        
        $this->state ^= AbstractPromise::PENDING | AbstractPromise::SUCCEEDED;
        $this->result = $value;
        
        if ($this instanceof Cancellable) {
            $this->context->unregisterCancellation($this);
        }
        
        if ($callbacks) {
            if (\is_array($callbacks)) {
                foreach ($callbacks as $callback) {
                    try {
                        $callback(null, $value);
                    } catch (\Throwable $e) {
                        $this->context->handleError($e);
                    }
                }
            } else {
                try {
                    $callbacks(null, $value);
                } catch (\Throwable $e) {
                    $this->context->handleError($e);
                }
            }
        }
    }