Вот упрощенная версия моего текущего кода:
Код: Выделить всё
func asyncWrap() async throws -> Int {
let cancellationQueue = DispatchQueue(label: "cancellation.queue", qos: .background)
let cancellationSemaphore = DispatchSemaphore(value: 0)
return try await withTaskCancellationHandler {
return try await withCheckedThrowingContinuation { continuation in
AsyncKt.kotlinMultiplatformCallback(onCancellation: { cancel in
cancellationQueue.async {
// Await cancellation signal
cancellationSemaphore.wait()
cancel()
}
}, completionHandler: { result, error in
cancellationSemaphore.signal()
if let ret = result?.intValue {
continuation.resume(returning: ret)
} else {
continuation.resume(throwing: error ?? fatalError())
}
})
}
} onCancel: {
// Handle task cancellation
cancellationSemaphore.signal()
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... sync-await