Generic Function

withTaskCancellationHandler(operation:onCancel:)

Execute an operation with a cancellation handler that’s immediately invoked if the current task is canceled. 現在のタスクが取り消されるならば直ちに発動されるある取り消しハンドラを備えた操作を遂行します。

Declaration 宣言

func withTaskCancellationHandler<T>(operation: () async throws -> T, onCancel handler: @Sendable () -> Void) async rethrows -> T

Discussion 解説

This differs from the operation cooperatively checking for cancellation and reacting to it in that the cancellation handler is always and immediately invoked when the task is canceled. For example, even if the operation is running code that never checks for cancellation, a cancellation handler still runs and provides a chance to run some cleanup code. これは、協力して取り消しを調べるそしてそれに反応する演算とは異なります、タスクが取り消される時にある取り消しハンドラが常にそして直ぐに発動される点において。例えば、たとえ取り消しを決して調べないコードを演算が実行しているとしても、ある取り消しハンドラが依然として動作してそしてあるクリーンアップコードを実行する機会を提供します。

Doesn’t check for cancellation, and always executes the passed operation. 取り消しを調べないでください、そして常に渡されたoperationを遂行してください。

This function returns immediately and never suspends. この関数は、直ぐに返ります、そして決して中断しません。

See Also 参照

Canceling Tasks タスクの取り消し