---
title: ThrowingTaskGroup
framework: swift
role: symbol
role_heading: Structure
path: swift/throwingtaskgroup
---

# ThrowingTaskGroup

A group that contains throwing, dynamically created child tasks.

## Declaration

```swift
@frozen struct ThrowingTaskGroup<ChildTaskResult, Failure> where ChildTaskResult : Sendable, Failure : Error
```

## Overview

Overview 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. note: TaskGroup note: DiscardingTaskGroup note: ThrowingDiscardingTaskGroup

## Topics

### Adding Tasks to a Throwing Task Group

- [addTask(priority:operation:)](swift/throwingtaskgroup/addtask(priority:operation:).md)
- [addTask(executorPreference:priority:operation:)](swift/throwingtaskgroup/addtask(executorpreference:priority:operation:).md)
- [addTask(name:priority:operation:)](swift/throwingtaskgroup/addtask(name:priority:operation:).md)
- [addTask(name:executorPreference:priority:operation:)](swift/throwingtaskgroup/addtask(name:executorpreference:priority:operation:).md)
- [addTaskUnlessCancelled(priority:operation:)](swift/throwingtaskgroup/addtaskunlesscancelled(priority:operation:).md)
- [addTaskUnlessCancelled(name:executorPreference:priority:operation:)](swift/throwingtaskgroup/addtaskunlesscancelled(name:executorpreference:priority:operation:).md)
- [addTaskUnlessCancelled(executorPreference:priority:operation:)](swift/throwingtaskgroup/addtaskunlesscancelled(executorpreference:priority:operation:).md)
- [addTaskUnlessCancelled(name:priority:operation:)](swift/throwingtaskgroup/addtaskunlesscancelled(name:priority:operation:).md)
- [addImmediateTask(name:priority:executorPreference:operation:)](swift/throwingtaskgroup/addimmediatetask(name:priority:executorpreference:operation:).md)
- [addImmediateTaskUnlessCancelled(name:priority:executorPreference:operation:)](swift/throwingtaskgroup/addimmediatetaskunlesscancelled(name:priority:executorpreference:operation:).md)

### Accessing Individual Results

- [next()](swift/throwingtaskgroup/next().md)
- [nextResult(isolation:)](swift/throwingtaskgroup/nextresult(isolation:).md)
- [next(isolation:)](swift/throwingtaskgroup/next(isolation:).md)
- [isEmpty](swift/throwingtaskgroup/isempty.md)
- [waitForAll(isolation:)](swift/throwingtaskgroup/waitforall(isolation:).md)

### Accessing an Asynchronous Sequence of Results

- [makeAsyncIterator()](swift/throwingtaskgroup/makeasynciterator().md)
- [allSatisfy(_:)](swift/throwingtaskgroup/allsatisfy(_:).md)
- [compactMap(_:)](swift/throwingtaskgroup/compactmap(_:)-944nh.md)
- [compactMap(_:)](swift/throwingtaskgroup/compactmap(_:)-7mgi5.md)
- [contains(_:)](swift/throwingtaskgroup/contains(_:).md)
- [contains(where:)](swift/throwingtaskgroup/contains(where:).md)
- [drop(while:)](swift/throwingtaskgroup/drop(while:).md)
- [dropFirst(_:)](swift/throwingtaskgroup/dropfirst(_:).md)
- [filter(_:)](swift/throwingtaskgroup/filter(_:).md)
- [first(where:)](swift/throwingtaskgroup/first(where:).md)
- [map(_:)](swift/throwingtaskgroup/map(_:)-58nrv.md)
- [map(_:)](swift/throwingtaskgroup/map(_:)-4a4ju.md)
- [max()](swift/throwingtaskgroup/max().md)
- [max(by:)](swift/throwingtaskgroup/max(by:).md)
- [min()](swift/throwingtaskgroup/min().md)
- [min(by:)](swift/throwingtaskgroup/min(by:).md)
- [prefix(_:)](swift/throwingtaskgroup/prefix(_:).md)
- [prefix(while:)](swift/throwingtaskgroup/prefix(while:).md)
- [reduce(_:_:)](swift/throwingtaskgroup/reduce(_:_:).md)
- [reduce(into:_:)](swift/throwingtaskgroup/reduce(into:_:).md)

### Canceling Tasks

- [isCancelled](swift/throwingtaskgroup/iscancelled.md)
- [cancelAll()](swift/throwingtaskgroup/cancelall().md)

### Supporting Types

- [ThrowingTaskGroup.Element](swift/throwingtaskgroup/element.md)
- [ThrowingTaskGroup.Iterator](swift/throwingtaskgroup/iterator.md)
- [ThrowingTaskGroup.AsyncIterator](swift/throwingtaskgroup/asynciterator.md)

### Deprecated

- [add(priority:operation:)](swift/throwingtaskgroup/add(priority:operation:).md)
- [async(priority:operation:)](swift/throwingtaskgroup/async(priority:operation:).md)
- [asyncUnlessCancelled(priority:operation:)](swift/throwingtaskgroup/asyncunlesscancelled(priority:operation:).md)
- [spawn(priority:operation:)](swift/throwingtaskgroup/spawn(priority:operation:).md)
- [spawnUnlessCancelled(priority:operation:)](swift/throwingtaskgroup/spawnunlesscancelled(priority:operation:).md)

### Default Implementations

- [AsyncSequence Implementations](swift/throwingtaskgroup/asyncsequence-implementations.md)

## Relationships

### Conforms To

- [AsyncSequence](swift/asyncsequence.md)
- [BitwiseCopyable](swift/bitwisecopyable.md)
- [Copyable](swift/copyable.md)
- [Escapable](swift/escapable.md)

## See Also

### Tasks

- [Task](swift/task.md)
- [TaskGroup](swift/taskgroup.md)
- [withTaskGroup(of:returning:isolation:body:)](swift/withtaskgroup(of:returning:isolation:body:).md)
- [withThrowingTaskGroup(of:returning:isolation:body:)](swift/withthrowingtaskgroup(of:returning:isolation:body:).md)
- [TaskPriority](swift/taskpriority.md)
- [DiscardingTaskGroup](swift/discardingtaskgroup.md)
- [withDiscardingTaskGroup(returning:isolation:body:)](swift/withdiscardingtaskgroup(returning:isolation:body:).md)
- [ThrowingDiscardingTaskGroup](swift/throwingdiscardingtaskgroup.md)
- [withThrowingDiscardingTaskGroup(returning:isolation:body:)](swift/withthrowingdiscardingtaskgroup(returning:isolation:body:).md)
- [UnsafeCurrentTask](swift/unsafecurrenttask.md)
