<!--
{
  "availability" : [
    "iOS: 26.4.0 -",
    "iPadOS: 26.4.0 -",
    "macCatalyst: -",
    "macOS: 26.4.0 -",
    "tvOS: 26.4.0 -",
    "visionOS: 26.4.0 -",
    "watchOS: 26.4.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppIntents",
  "identifier" : "/documentation/AppIntents/CancellableIntent/withIntentCancellationHandler(operation:onCancel:isolation:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "App Intents",
      "AppIntents"
    ],
    "preciseIdentifier" : "s:10AppIntents17CancellableIntentPAAE04withD19CancellationHandler9operation8onCancel9isolationqd__qd__yYaKXE_yAA0dF6ReasonVYbXEScA_pSgYitYaKlF"
  },
  "title" : "withIntentCancellationHandler(operation:onCancel:isolation:)"
}
-->

# withIntentCancellationHandler(operation:onCancel:isolation:)

Runs an operation with a cancellation handler that receives a cancellation reason.

```
func withIntentCancellationHandler<T>(operation: () async throws -> T, onCancel handler: @Sendable (IntentCancellationReason) -> Void, isolation: isolated (any Actor)? = #isolation) async rethrows -> T
```

## Parameters

`operation`

The closure with the operation to run. Use this closure to run the
code for your app intent’s task.

`handler`

The closure to run when cancellation occurs. Use this closure to respond
to the cancellation and perform any required cleanup. The closure receives a parameter
with the cancellation reason and returns no value.

`isolation`

The actor isolation for the operation. If you don’t specify a value, the
system uses the current isolation context.

## Return Value

The value you return from the `operation` closure.

## Discussion> Throws: Rethrows any error thrown by the operation.

Use this method to handle cancellation in an app intent’s [`perform()`](/documentation/AppIntents/AppIntent/perform()) method
when you also want to know the reason for cancellation. If cancellation occurs due to a
timeout or someone canceling the operation, the system runs your `handler` and gives you
a little extra time to perform any cleanup. If you don’t need to know the cancellation reason,
you can use the standard Swift <doc://com.apple.documentation/documentation/Swift/withTaskCancellationHandler(handler:operation:)>
method for tasks instead.

---

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)