Skip to content

Instantly share code, notes, and snippets.

@michaelbukachi
Created December 4, 2019 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelbukachi/fad5d6f9f3313d899325e5d7a2403603 to your computer and use it in GitHub Desktop.
Save michaelbukachi/fad5d6f9f3313d899325e5d7a2403603 to your computer and use it in GitHub Desktop.
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