Automattic\WooCommerce\Vendor\GraphQL\Executor

ReferenceExecutor::promiseReduceprotectedWC 1.0

Similar to array_reduce(), however the reducing callback may return a Promise, in which case reduction will continue after each promise resolves.

If the callback does not return a Promise, then this function will also not return a Promise.

Method of the class: ReferenceExecutor{}

No Hooks.

Returns

Promise|Mixed|null.

Usage

// protected - for code of main (parent) or child class
$result = $this->promiseReduce( $values, $callback, $initialValue );
$values(array) (required)
.
$callback(callable) (required)
.
$initialValue(Promise|mixed|null) (required)
.

ReferenceExecutor::promiseReduce() code WC 10.9.1

protected function promiseReduce(array $values, callable $callback, $initialValue)
{
    return array_reduce(
        $values,
        function ($previous, $value) use ($callback) {
            $promise = $this->getPromise($previous);
            if ($promise !== null) {
                return $promise->then(static fn ($resolved) => $callback($resolved, $value));
            }

            return $callback($previous, $value);
        },
        $initialValue
    );
}