Skip to content

Commit 3ffb339

Browse files
authored
[Docs][Concurrency] TaskGroup docs: waitForAll (non-)cancellation (#63956)
1 parent 46bf41c commit 3ffb339

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

stdlib/public/Concurrency/TaskGroup.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,35 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
616616
}
617617

618618
/// Wait for all of the group's remaining tasks to complete.
619+
///
620+
/// If any of the tasks throw, the *first* error thrown is captured
621+
/// and re-thrown by this method although the task group is *not* cancelled
622+
/// when this happens.
623+
///
624+
/// ### Cancelling the task group on first error
625+
///
626+
/// If you want to cancel the task group, and all "sibling" tasks,
627+
/// whenever any of child tasks throws an error, use the following pattern instead:
628+
///
629+
/// ```
630+
/// while !group.isEmpty {
631+
/// do {
632+
/// try await group.next()
633+
/// } catch is CancellationError {
634+
/// // we decide that cancellation errors thrown by children,
635+
/// // should not cause cancellation of the entire group.
636+
/// continue;
637+
/// } catch {
638+
/// // other errors though we print and cancel the group,
639+
/// // and all of the remaining child tasks within it.
640+
/// group.cancelAll()
641+
/// }
642+
/// }
643+
/// assert(group.isEmpty())
644+
/// ```
645+
///
646+
/// - Throws: The *first* error that was thrown by a child task during draining all the tasks.
647+
/// This first error is stored until all other tasks have completed, and is re-thrown afterwards.
619648
@_alwaysEmitIntoClient
620649
public mutating func waitForAll() async throws {
621650
var firstError: Error? = nil

0 commit comments

Comments
 (0)