Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Kotlin coroutines basic example.
// runs the code in the background thread pool
fun asyncOverlay() = async(CommonPool) {
// start two async operations
val original = asyncLoadImage("original")
val overlay = asyncLoadImage("overlay")
// and then apply overlay to both results
applyOverlay(original.await(), overlay.await())
}
// launches new coroutine in UI context
launch(UI) {
// wait for async overlay to complete
val image = asyncOverlay().await()
// and then show it in UI
showImage(image)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment