pending example

callable $onRejected = null
    ): PromiseInterface {
        // If there's no onRejected callback then just return self.         if (!$onRejected) {
            return $this;
        }

        $queue = Utils::queue();
        $reason = $this->reason;
        $p = new Promise([$queue, 'run']);
        $queue->add(static function D) use ($p$reason$onRejected): void {
            if (Is::pending($p)) {
                try {
                    // Return a resolved promise if onRejected does not throw.                     $p->resolve($onRejected($reason));
                } catch (\Throwable $e) {
                    // onRejected threw, so return a rejected promise.                     $p->reject($e);
                }
            }
        });

        return $p;
    }
callable $onRejected = null
    ): PromiseInterface {
        // Return itself if there is no onFulfilled function.         if (!$onFulfilled) {
            return $this;
        }

        $queue = Utils::queue();
        $p = new Promise([$queue, 'run']);
        $value = $this->value;
        $queue->add(static function D) use ($p$value$onFulfilled): void {
            if (Is::pending($p)) {
                try {
                    $p->resolve($onFulfilled($value));
                } catch (\Throwable $e) {
                    $p->reject($e);
                }
            }
        });

        return $p;
    }

    

    public static function task(callable $task): PromiseInterface
    {
        $queue = self::queue();
        $promise = new Promise([$queue, 'run']);
        $queue->add(function D) use ($task$promise): void {
            try {
                if (Is::pending($promise)) {
                    $promise->resolve($task());
                }
            } catch (\Throwable $e) {
                $promise->reject($e);
            }
        });

        return $promise;
    }

    /** * Synchronously waits on a promise to resolve and returns an inspection * state array. * * Returns a state associative array containing a "state" key mapping to a * valid promise state. If the state of the promise is "fulfilled", the * array will contain a "value" key mapping to the fulfilled value of the * promise. If the promise is rejected, the array will contain a "reason" * key mapping to the rejection reason of the promise. * * @param PromiseInterface $promise Promise or value. */
// If the value was not a settled promise or a thenable, then resolve         // it in the task queue using the correct ID.         if (!is_object($value) || !method_exists($value, 'then')) {
            $id = $state === self::FULFILLED ? 1 : 2;
            // It's a success, so resolve the handlers in the queue.             Utils::queue()->add(static function D) use ($id$value$handlers): void {
                foreach ($handlers as $handler) {
                    self::callHandler($id$value$handler);
                }
            });
        } elseif ($value instanceof Promise && Is::pending($value)) {
            // We can just merge our handlers onto the next promise.             $value->handlers = array_merge($value->handlers, $handlers);
        } else {
            // Resolve the handlers when the forwarded promise is resolved.             $value->then(
                static function D$value) use ($handlers): void {
                    foreach ($handlers as $handler) {
                        self::callHandler(1, $value$handler);
                    }
                },
                static function D$reason) use ($handlers): void {
                    
Home | Imprint | This part of the site doesn't use cookies.