ComponentsTaskGroup::awaitCompletion

TaskGroup::awaitCompletion ​

(PHP 8.6+, True Async 1.0)

php
public TaskGroup::awaitCompletion(): void

Waits until all tasks in the group have fully completed. Unlike all(), it does not return results and does not throw exceptions on task errors.

The group must be closed before calling this method.

A typical use case is waiting for coroutines to actually finish after cancel(). The cancel() method initiates cancellation, but coroutines may finish asynchronously. awaitCompletion() guarantees that all coroutines have stopped.

Errors ​

Throws Async\AsyncException if the group is not closed.

Examples ​

Example #1 Waiting after cancel ​

php
<?php

use Async\TaskGroup;
use function Async\suspend;

spawn(function() {
    $group = new TaskGroup();

    $group->spawn(function() {
        suspend();
        return "result";
    });

    $group->cancel();
    $group->awaitCompletion(Async\timeout(5000));

    echo "all coroutines finished\n";
    var_dump($group->isFinished()); // bool(true)
});

Example #2 Getting results after waiting ​

php
<?php

use Async\TaskGroup;

spawn(function() {
    $group = new TaskGroup();

    $group->spawn(fn() => "ok");
    $group->spawn(fn() => throw new \RuntimeException("fail"));

    $group->close();
    $group->awaitCompletion(Async\timeout(5000));

    // No exceptions — check manually
    $results = $group->getResults();
    $errors = $group->getErrors();

    echo "Successful: " . count($results) . "\n"; // 1
    echo "Errors: " . count($errors) . "\n";      // 1
});

See Also ​