Mikolaj
11/08/2024, 1:15 PMsuspendCancellableCoroutine
there is an example of usage with callback (example in 🧵).
Will incokeOnCancellation
be called only when coroutine is cancelled? Or does calling resumeWithException
also invokes the incokeOnCancellation
block?Mikolaj
11/08/2024, 1:16 PMsuspend fun awaitCallback(): T = suspendCancellableCoroutine { continuation ->
val callback = object : Callback {
override fun onCompleted(value: T) {
continuation.resume(value)
}
override fun onApiError(cause: Throwable) {
continuation.resumeWithException(cause)
}
}
api.register(callback)
continuation.invokeOnCancellation { api.unregister(callback) }
}
baxter
11/08/2024, 8:33 PMbaxter
11/08/2024, 8:37 PMCancellableContinuation
object used by suspendCancellableCoroutine
also has a resume()
method that takes both the value you want to resume with, as well as a lambda to run if the value was cancelled when the value was not used.
https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-cancellable-continuation/#499398008%2FFu[…]ns%2F1975948010