Last active
October 10, 2020 16:55
-
-
Save Slowhand0309/e8d397644131f5a06a58e4ab870b741d to your computer and use it in GitHub Desktop.
[Callback to Coroutines] use suspendCoroutine. #Kotlin
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
/* | |
before | |
fun asyncLoad(callback: (result: String) -> Unit) { | |
load { result -> | |
callback(result) | |
} | |
} | |
*/ | |
suspend fun asyncLoad(): String = suspendCoroutine { continuation -> | |
load { result -> | |
if (result.isEmpty()) { | |
continuation.resumeWithException(LoadError()) | |
} else { | |
continuation.resume(result) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment