Skip to content

Instantly share code, notes, and snippets.

@Slowhand0309
Last active October 10, 2020 16:55
Show Gist options
  • Save Slowhand0309/e8d397644131f5a06a58e4ab870b741d to your computer and use it in GitHub Desktop.
Save Slowhand0309/e8d397644131f5a06a58e4ab870b741d to your computer and use it in GitHub Desktop.
[Callback to Coroutines] use suspendCoroutine. #Kotlin
/*
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