Contents

waitForAll(isolation:)

Wait for all of the group’s remaining tasks to complete.

Declaration

mutating func waitForAll(isolation: isolated (any Actor)? = #isolation) async throws

Discussion

If any of the tasks throw, the first error thrown is captured and re-thrown by this method although the task group is not canceled when this happens.

Cancelling the task group on first error

If you want to cancel the task group, and all “sibling” tasks, whenever any of child tasks throws an error, use the following pattern instead:

while !group.isEmpty {
    do {
        try await group.next()
    } catch is CancellationError {
        // we decide that cancellation errors thrown by children,
        // should not cause cancellation of the entire group.
        continue;
    } catch {
        // other errors though we print and cancel the group,
        // and all of the remaining child tasks within it.
        print("Error: \(error)")
        group.cancelAll()
    }
}
assert(group.isEmpty())

See Also

Accessing Individual Results