---
title: onTermination
framework: swift
role: symbol
role_heading: Instance Property
path: swift/asyncstream/continuation/ontermination
---

# onTermination

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

## Declaration

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

## Discussion

Discussion If an onTermination callback is set, using task cancellation to terminate iteration of an AsyncStream results in a call to this callback. Canceling an active iteration invokes the onTermination callback first, then resumes by yielding nil. This means that you can perform needed cleanup in the cancellation handler. After reaching a terminal state as a result of cancellation, the AsyncStream sets the callback to nil. 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 withTaskCancellationHandler(operation:onCancel:).

## See Also

### Handling Termination

- [AsyncStream.Continuation.Termination](swift/asyncstream/continuation/termination.md)
