<!--
{
  "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/AsyncThrowingStream/Continuation/onTermination",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Property",
  "symbol" : {
    "kind" : "Instance Property",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Scs12ContinuationV13onTerminationyAB0C0Oyxq___GYbcSgvp"
  },
  "title" : "onTermination"
}
-->

# onTermination

A callback to invoke when canceling iteration of an asynchronous
stream.

```
var onTermination: (@Sendable (AsyncThrowingStream<Element, Failure>.Continuation.Termination) -> Void)? { get nonmutating set }
```

## Discussion

If an `onTermination` callback is set, using task cancellation to
terminate iteration of an `AsyncThrowingStream` results in a call to this
callback.

Canceling an active iteration invokes the `onTermination` callback
first, and then resumes by yielding `nil` or throwing an error from the
iterator. This means that you can perform needed cleanup in the
cancellation handler. After reaching a terminal state, the
`AsyncThrowingStream` disposes of the callback.

> Note: Because the system might call the `onTermination` callback as
> part of task cancellation, it’s subject to the same considerations for
> avoiding deadlock as outlined in the documentation for
> ``doc://com.apple.Swift/documentation/Swift/withTaskCancellationHandler(operation:onCancel:)``.

---

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)