Skip to content

Instantly share code, notes, and snippets.

@gzoritchak
Last active March 1, 2017 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gzoritchak/2b8ee80d64c062dc728021b1831d00b5 to your computer and use it in GitHub Desktop.
Save gzoritchak/2b8ee80d64c062dc728021b1831d00b5 to your computer and use it in GitHub Desktop.
// 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