any()

/**
 * @throws Throwable
 * @param array<int, Async|Promise|callable> $promises
 * @phpstan-param array<int, Async|Promise|callable> $promises
 */
public static function any(array $promises): Promise;

The Promise::any() static method takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when any of the input's promises fulfills, with this first fulfillment value. It rejects when all of the input's promises reject (including when an empty iterable is passed), with an Error containing an array of rejection reasons.

Example:

function testPromise1() : Promise { 
    return new Promise(function ($resolve, $reject) {
        $resolve("A");
    });
}

function testPromise2() : Promise {
    return new Promise(function ($resolve, $reject) {
        $reject("B");
    });
}

Promise::any([
	testPromise1(),
	testPromise2()
])->then(fn($result) => var_dump($promise));

Output:

A

Last updated