Created
December 4, 2019 13:37
-
-
Save michaelbukachi/fad5d6f9f3313d899325e5d7a2403603 to your computer and use it in GitHub Desktop.
callback to kotlin coroutine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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