promiseFor example

$this->result->reject($reason);
    }

    public function cancel(): void
    {
        $this->currentPromise->cancel();
        $this->result->cancel();
    }

    private function nextCoroutine($yielded): void
    {
        $this->currentPromise = Create::promiseFor($yielded)
            ->then([$this, '_handleSuccess'][$this, '_handleFailure']);
    }

    /** * @internal */
    public function _handleSuccess($value): void
    {
        unset($this->currentPromise);
        try {
            $next = $this->generator->send($value);
            
&& $this->advanceIterator()
            && $this->addPending()) {
        }
    }

    private function addPending(): bool
    {
        if (!$this->iterable || !$this->iterable->valid()) {
            return false;
        }

        $promise = Create::promiseFor($this->iterable->current());
        $key = $this->iterable->key();

        // Iterable keys may not be unique, so we use a counter to         // guarantee uniqueness         $idx = $this->nextPendingIndex++;

        $this->pending[$idx] = $promise->then(
            function D$value) use ($idx$key): void {
                if ($this->onFulfilled) {
                    call_user_func(
                        $this->onFulfilled,
                        


        return $result;
    }

    private function wrapThenCallback(?callable $callback): ?callable
    {
        if (null === $callback) {
            return null;
        }

        return static fn ($value) => Create::promiseFor($callback($value));
    }
}
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) {
            $promise = Create::promiseFor($this->result);

            return $onFulfilled ? $promise->then($onFulfilled) : $promise;
        }

        // It's either cancelled or rejected, so return a rejected promise         // and immediately invoke any callbacks.         $rejection = Create::rejectionFor($this->result);

        return $onRejected ? $rejection->then(null, $onRejected) : $rejection;
    }

    

    private function transfer(RequestInterface $request, array $options): PromiseInterface
    {
        $request = $this->applyOptions($request$options);
        /** @var HandlerStack $handler */
        $handler = $options['handler'];

        try {
            return P\Create::promiseFor($handler($request$options));
        } catch (\Exception $e) {
            return P\Create::rejectionFor($e);
        }
    }

    /** * Applies the array of request options to a request. */
    private function applyOptions(RequestInterface $request, array &$options): RequestInterface
    {
        $modify = [
            
$msg = 'An error was encountered during the on_headers event';
                $response = new RequestException($msg$request$response$e);
            }
        }

        if (\is_callable($response)) {
            $response = $response($request$options);
        }

        $response = $response instanceof \Throwable
            ? P\Create::rejectionFor($response)
            : P\Create::promiseFor($response);

        return $response->then(
            function D?ResponseInterface $value) use ($request$options) {
                $this->invokeStats($request$options$value);
                if ($this->onFulfilled) {
                    ($this->onFulfilled)($value);
                }

                if ($value !== null && isset($options['sink'])) {
                    $contents = (string) $value->getBody();
                    $sink = $options['sink'];

                    
Home | Imprint | This part of the site doesn't use cookies.