Skip to content

Instantly share code, notes, and snippets.

@walmyrcarvalho
Last active March 1, 2017 12:38
Show Gist options
  • Save walmyrcarvalho/fa32e15a3c4e1a2b51384f63b9359482 to your computer and use it in GitHub Desktop.
Save walmyrcarvalho/fa32e15a3c4e1a2b51384f63b9359482 to your computer and use it in GitHub Desktop.
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