await_any_of

(PHP 8.6+, True Async 1.0)

await_any_of() — Waits for the first N tasks to complete, collecting results and errors separately. Does not throw an exception when individual tasks fail.

Description

await_any_of(
    int $count,
    iterable $triggers,
    ?Async\Awaitable $cancellation = null,
    bool $preserveKeyOrder = true,
    bool $fillNull = false
): array

Parameters

count The number of successful results to wait for.

triggers An iterable collection of Async\Completable objects.

cancellation An optional Awaitable to cancel the wait.

preserveKeyOrder If true, result keys correspond to the input array keys.

fillNull If true, null is placed in the results array for tasks that failed.

Return Values

An array of two elements: [$results, $errors]

Examples

Example #1 Quorum with error tolerance

<?php
use function Async\spawn;
use function Async\await_any_of;

$nodes = ['node1', 'node2', 'node3', 'node4', 'node5'];

$coroutines = [];
foreach ($nodes as $node) {
    $coroutines[$node] = spawn(file_get_contents(...), "https://$node/vote");
}

// Wait for quorum: 3 out of 5 responses
[$results, $errors] = await_any_of(3, $coroutines);

if (count($results) >= 3) {
    echo "Quorum reached\n";
} else {
    echo "Quorum not reached, errors: " . count($errors) . "\n";
}
?>

Notes

Note: The triggers parameter accepts any iterable, including Iterator implementations. See the Iterator example.

See Also