Kotlin coroutines basic example.
This file contains 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
// 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