I am trying to write a suspend function that can l...
# coroutines
v
I am trying to write a suspend function that can listen to cancellation of continuation, and invoke a suspended callback. Here is what I am looking at:
Copy code
suspend fun <T> PreparedStatement.withCancellation(callback: suspend () -> T): T {
    val stmt = this
    suspendCancellableCoroutine { cont ->
        cont.invokeOnCancellation { 
            stmt.cancel()
        }
    }

    return callback()
}
z
Copy code
try {
  awaitCancellation() 
} finally {
  this.cancel()
  callback()
}