Skip to content

Instantly share code, notes, and snippets.

@soudmaijer
Created April 28, 2021 09:30
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 soudmaijer/aff94635f1ccb4745f3e273f2656b623 to your computer and use it in GitHub Desktop.
Save soudmaijer/aff94635f1ccb4745f3e273f2656b623 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
suspend fun loadImage200(name: String) {
delay(2000) // simulate slow behaviour
}
fun loadImage404(name: String) {
throw RuntimeException("Image not found: $name")
}
fun main() {
runBlocking {
try {
val image1 = async { loadImage200("image1") }
val image2 = async { loadImage404("image2") }
image1.await()
image2.await()
} catch (e: Exception) {
println(e)
}
println("Sleeping...")
Thread.sleep(5000)
println("Loading images done.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment