<!--
{
  "availability" : [
    "iOS: 26.0.0 -",
    "iPadOS: 26.0.0 -",
    "macCatalyst: 26.0.0 -",
    "macOS: 26.0.0 -",
    "tvOS: 26.0.0 -",
    "visionOS: 26.0.0 -",
    "watchOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/withTaskPriorityEscalationHandler(operation:onPriorityEscalated:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:12_Concurrency33withTaskPriorityEscalationHandler9operation02onD9EscalatedxxyYaq_YKYCXE_yScP_ScPtYbXEtYaq_YKs5ErrorR_r0_lF"
  },
  "title" : "withTaskPriorityEscalationHandler(operation:onPriorityEscalated:)"
}
-->

# withTaskPriorityEscalationHandler(operation:onPriorityEscalated:)

Runs the passed `operation` while registering a task priority escalation handler.
The handler will be triggered concurrently to the current task if the current
is subject to priority escalation.

```
nonisolated(nonsending) func withTaskPriorityEscalationHandler<T, E>(operation: nonisolated(nonsending) () async throws(E) -> T, onPriorityEscalated handler: @Sendable (TaskPriority, TaskPriority) -> Void) async throws(E) -> T where E : Error
```

## Parameters

`operation`

the operation during which to listen for priority escalation

`handler`

handler to invoke, concurrently to `operation`,
when priority escalation happens.
The first argument is the old priority (before escalation),
and the second argument is the new escalated priority.

## Return Value

the value returned by `operation`

## Discussion

The handler may perform additional actions upon priority escalation,
but cannot influence how the escalation influences the task, i.e. the task’s
priority will be escalated regardless of actions performed in the handler.

The handler will only trigger if a priority escalation occurs while the
operation is in progress.

If multiple task escalation handlers are nested they will all be triggered.

Task escalation propagates through structured concurrency child-tasks.

> Throws: when the `operation` throws an error

---

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)