<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/ThrowingTaskGroup",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Scg"
  },
  "title" : "ThrowingTaskGroup"
}
-->

# ThrowingTaskGroup

A group that contains throwing, dynamically created child tasks.

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

## 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/Swift/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()`](/documentation/Swift/ThrowingTaskGroup/cancelAll()) is invoked on it,
- when an error is thrown out of the `withThrowingTaskGroup(...) { }` closure,
- when the [`Task`](/documentation/Swift/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:operation:)`](/documentation/Swift/ThrowingTaskGroup/addTaskUnlessCancelled(priority:operation:))
rather than the plain [`addTask(priority:operation:)`](/documentation/Swift/ThrowingTaskGroup/addTask(priority:operation:)) which adds tasks unconditionally.

For information about the language-level concurrency model that `ThrowingTaskGroup` is part of,
see [Concurrency](https://docs.swift.org/swift-book/LanguageGuide/Concurrency.html) in [The Swift Programming Language](https://docs.swift.org/swift-book/).

> SeeAlso: ``doc://com.apple.Swift/documentation/Swift/TaskGroup``

> SeeAlso: ``doc://com.apple.Swift/documentation/Swift/DiscardingTaskGroup``

> SeeAlso: ``doc://com.apple.Swift/documentation/Swift/ThrowingDiscardingTaskGroup``

## Topics

### Adding Tasks to a Throwing Task Group

[`addTask(priority:operation:)`](/documentation/Swift/ThrowingTaskGroup/addTask(priority:operation:))

Adds a child task to the group.

[`addTask(executorPreference:priority:operation:)`](/documentation/Swift/ThrowingTaskGroup/addTask(executorPreference:priority:operation:))

Adds a child task to the group.

[`addTask(name:priority:operation:)`](/documentation/Swift/ThrowingTaskGroup/addTask(name:priority:operation:))

Adds a child task to the group.

[`addTask(name:executorPreference:priority:operation:)`](/documentation/Swift/ThrowingTaskGroup/addTask(name:executorPreference:priority:operation:))

Adds a child task to the group.

[`addTaskUnlessCancelled(priority:operation:)`](/documentation/Swift/ThrowingTaskGroup/addTaskUnlessCancelled(priority:operation:))

Adds a child task to the group, unless the group has been canceled.
Returns a boolean value indicating if the task was successfully added to the group or not.

[`addTaskUnlessCancelled(name:executorPreference:priority:operation:)`](/documentation/Swift/ThrowingTaskGroup/addTaskUnlessCancelled(name:executorPreference:priority:operation:))

Adds a child task to the group, unless the group has been canceled.
Returns a boolean value indicating if the task was successfully added to the group or not.

[`addTaskUnlessCancelled(executorPreference:priority:operation:)`](/documentation/Swift/ThrowingTaskGroup/addTaskUnlessCancelled(executorPreference:priority:operation:))

Adds a child task to the group, unless the group has been canceled.
Returns a boolean value indicating if the task was successfully added to the group or not.

[`addTaskUnlessCancelled(name:priority:operation:)`](/documentation/Swift/ThrowingTaskGroup/addTaskUnlessCancelled(name:priority:operation:))

Adds a child task to the group, unless the group has been canceled.
Returns a boolean value indicating if the task was successfully added to the group or not.

[`addImmediateTask(name:priority:executorPreference:operation:)`](/documentation/Swift/ThrowingTaskGroup/addImmediateTask(name:priority:executorPreference:operation:))

Add a child task to the group and immediately start running it in the context of the calling thread/task.

[`addImmediateTaskUnlessCancelled(name:priority:executorPreference:operation:)`](/documentation/Swift/ThrowingTaskGroup/addImmediateTaskUnlessCancelled(name:priority:executorPreference:operation:))

Add a child task to the group and immediately start running it in the context of the calling thread/task.

### Accessing Individual Results

[`next()`](/documentation/Swift/ThrowingTaskGroup/next())

[`nextResult()`](/documentation/Swift/ThrowingTaskGroup/nextResult())

Wait for the next child task to complete,
and return a result containing either
the value that the child task returned or the error that it threw.

[`next(isolation:)`](/documentation/Swift/ThrowingTaskGroup/next(isolation:))

Wait for the next child task to complete,
and return the value it returned or rethrow the error it threw.

[`isEmpty`](/documentation/Swift/ThrowingTaskGroup/isEmpty)

A Boolean value that indicates whether the group has any remaining tasks.

[`waitForAll()`](/documentation/Swift/ThrowingTaskGroup/waitForAll())

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

### Accessing an Asynchronous Sequence of Results

[`makeAsyncIterator()`](/documentation/Swift/ThrowingTaskGroup/makeAsyncIterator())

Creates the asynchronous iterator that produces elements of this
asynchronous sequence.

[`allSatisfy(_:)`](/documentation/Swift/ThrowingTaskGroup/allSatisfy(_:))

Returns a Boolean value that indicates whether all elements produced by the
asynchronous sequence satisfy the given predicate.

[`compactMap(_:)`](/documentation/Swift/ThrowingTaskGroup/compactMap(_:)-944nh)

Creates an asynchronous sequence that maps an error-throwing closure over
the base sequence’s elements, omitting results that don’t return a value.

[`compactMap(_:)`](/documentation/Swift/ThrowingTaskGroup/compactMap(_:)-7mgi5)

Creates an asynchronous sequence that maps the given closure over the
asynchronous sequence’s elements, omitting results that don’t return a
value.

[`contains(_:)`](/documentation/Swift/ThrowingTaskGroup/contains(_:))

Returns a Boolean value that indicates whether the asynchronous sequence
contains the given element.

[`contains(where:)`](/documentation/Swift/ThrowingTaskGroup/contains(where:))

Returns a Boolean value that indicates whether the asynchronous sequence
contains an element that satisfies the given predicate.

[`drop(while:)`](/documentation/Swift/ThrowingTaskGroup/drop(while:))

Omits elements from the base asynchronous sequence until a given closure
returns false, after which it passes through all remaining elements.

[`dropFirst(_:)`](/documentation/Swift/ThrowingTaskGroup/dropFirst(_:))

Omits a specified number of elements from the base asynchronous sequence,
then passes through all remaining elements.

[`filter(_:)`](/documentation/Swift/ThrowingTaskGroup/filter(_:))

Creates an asynchronous sequence that contains, in order, the elements of
the base sequence that satisfy the given predicate.

[`first(where:)`](/documentation/Swift/ThrowingTaskGroup/first(where:))

Returns the first element of the sequence that satisfies the given
predicate.

[`map(_:)`](/documentation/Swift/ThrowingTaskGroup/map(_:)-58nrv)

Creates an asynchronous sequence that maps the given error-throwing
closure over the asynchronous sequence’s elements.

[`map(_:)`](/documentation/Swift/ThrowingTaskGroup/map(_:)-4a4ju)

Creates an asynchronous sequence that maps the given closure over the
asynchronous sequence’s elements.

[`max()`](/documentation/Swift/ThrowingTaskGroup/max())

Returns the maximum element in an asynchronous sequence of comparable
elements.

[`max(by:)`](/documentation/Swift/ThrowingTaskGroup/max(by:))

Returns the maximum element in the asynchronous sequence, using the given
predicate as the comparison between elements.

[`min()`](/documentation/Swift/ThrowingTaskGroup/min())

Returns the minimum element in an asynchronous sequence of comparable
elements.

[`min(by:)`](/documentation/Swift/ThrowingTaskGroup/min(by:))

Returns the minimum element in the asynchronous sequence, using the given
predicate as the comparison between elements.

[`prefix(_:)`](/documentation/Swift/ThrowingTaskGroup/prefix(_:))

Returns an asynchronous sequence, up to the specified maximum length,
containing the initial elements of the base asynchronous sequence.

[`prefix(while:)`](/documentation/Swift/ThrowingTaskGroup/prefix(while:))

Returns an asynchronous sequence, containing the initial, consecutive
elements of the base sequence that satisfy the given predicate.

[`reduce(_:_:)`](/documentation/Swift/ThrowingTaskGroup/reduce(_:_:))

Returns the result of combining the elements of the asynchronous sequence
using the given closure.

[`reduce(into:_:)`](/documentation/Swift/ThrowingTaskGroup/reduce(into:_:))

Returns the result of combining the elements of the asynchronous sequence
using the given closure, given a mutable initial value.

### Canceling Tasks

[`isCancelled`](/documentation/Swift/ThrowingTaskGroup/isCancelled)

A Boolean value that indicates whether the group was canceled.

[`cancelAll()`](/documentation/Swift/ThrowingTaskGroup/cancelAll())

Cancel all of the remaining tasks in the group.

### Supporting Types

[`Element`](/documentation/Swift/ThrowingTaskGroup/Element)

The type of element produced by this asynchronous sequence.

[`Iterator`](/documentation/Swift/ThrowingTaskGroup/Iterator)

A type that provides an iteration interface
over the results of tasks added to the group.

[`AsyncIterator`](/documentation/Swift/ThrowingTaskGroup/AsyncIterator)

The type of asynchronous iterator that produces elements of this
asynchronous sequence.

### Deprecated

[`add(priority:operation:)`](/documentation/Swift/ThrowingTaskGroup/add(priority:operation:))

[`async(priority:operation:)`](/documentation/Swift/ThrowingTaskGroup/async(priority:operation:))

[`asyncUnlessCancelled(priority:operation:)`](/documentation/Swift/ThrowingTaskGroup/asyncUnlessCancelled(priority:operation:))

[`nextResult(isolation:)`](/documentation/Swift/ThrowingTaskGroup/nextResult(isolation:))

[`spawn(priority:operation:)`](/documentation/Swift/ThrowingTaskGroup/spawn(priority:operation:))

[`spawnUnlessCancelled(priority:operation:)`](/documentation/Swift/ThrowingTaskGroup/spawnUnlessCancelled(priority:operation:))

[`waitForAll(isolation:)`](/documentation/Swift/ThrowingTaskGroup/waitForAll(isolation:))

## Relationships

### Conforms To

[`BitwiseCopyable`](/documentation/Swift/BitwiseCopyable)

[`AsyncSequence`](/documentation/Swift/AsyncSequence)

[`Escapable`](/documentation/Swift/Escapable)

[`Copyable`](/documentation/Swift/Copyable)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)