Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
callback to kotlin coroutine
interface Callback<S> {
fun success(s: S)
fun error(e: Exception)
}
fun<T> doSomethingLong(callback: Callback<T>) {
}
private suspend fun <T> someFuncAwait() :T = suspendCancellableCoroutine {
val callback = object: Callback<T> {
override fun success(s: T) {
it.resume(s)
}
override fun error(e: Exception) {
it.resumeWithException(e)
}
} // callback is garbage collected before continuation is triggered
doSomethingLong(callback)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment