ThrowingTaskGroup
A group that contains throwing, dynamically created child tasks.
Declaration
@frozen struct ThrowingTaskGroup<ChildTaskResult, Failure> where ChildTaskResult : Sendable, Failure : ErrorOverview
To create a throwing task group, call the withThrowingTaskGroup(of:returning:body:) method.
Don’t use a task group from outside the task where you created it. In most cases, the Swift type system prevents a task group from escaping like that because adding a child task to a task group is a mutating operation, and mutation operations can’t be performed from concurrent execution contexts like a child task.
Refer to TaskGroup documentation for detailed discussion of semantics shared between all task groups.
Cancellation behavior
A task group becomes canceled in one of the following ways:
when cancelAll() is invoked on it,
when an error is thrown out of the
withThrowingTaskGroup(...) { }closure,when the Task running this task group is canceled.
Since a ThrowingTaskGroup is a structured concurrency primitive, cancellation is automatically propagated through all of its child-tasks (and their child tasks).
A canceled task group can still keep adding tasks, however they will start being immediately canceled, and may act accordingly to this. To avoid adding new tasks to an already canceled task group, use addTaskUnlessCancelled(priority:body:) rather than the plain addTask(priority:body:) which adds tasks unconditionally.
For information about the language-level concurrency model that ThrowingTaskGroup is part of, see Concurrency in The Swift Programming Language.
Topics
Adding Tasks to a Throwing Task Group
addTask(priority:operation:)addTask(executorPreference:priority:operation:)addTask(name:priority:operation:)addTask(name:executorPreference:priority:operation:)addTaskUnlessCancelled(priority:operation:)addTaskUnlessCancelled(name:executorPreference:priority:operation:)addTaskUnlessCancelled(executorPreference:priority:operation:)addTaskUnlessCancelled(name:priority:operation:)addImmediateTask(name:priority:executorPreference:operation:)addImmediateTaskUnlessCancelled(name:priority:executorPreference:operation:)
Accessing Individual Results
Accessing an Asynchronous Sequence of Results
makeAsyncIterator()allSatisfy(_:)compactMap(_:)compactMap(_:)contains(_:)contains(where:)drop(while:)dropFirst(_:)filter(_:)first(where:)map(_:)map(_:)max()max(by:)min()min(by:)prefix(_:)prefix(while:)reduce(_:_:)reduce(into:_:)
Canceling Tasks
Supporting Types
Deprecated
add(priority:operation:)async(priority:operation:)asyncUnlessCancelled(priority:operation:)spawn(priority:operation:)spawnUnlessCancelled(priority:operation:)