Skip to content

Instantly share code, notes, and snippets.

@mayojava
Last active October 26, 2018 10:28
Show Gist options
  • Select an option

  • Save mayojava/7a48acf5922e4371812ebe641e4a2f5d to your computer and use it in GitHub Desktop.

Select an option

Save mayojava/7a48acf5922e4371812ebe641e4a2f5d to your computer and use it in GitHub Desktop.
Custom Coroutine Scope
fun main() = runBlocking{
launchChildren()
println("never gets printed") // this line never gets printed
}
suspend fun launchChildren(): Int = coroutineScope {
val first = async {
try {
firstValue()
} finally {
println("got cancelled")
}
}
val second = async { secondValue() }
first.await() + second.await()
}
suspend fun firstValue(): Int {
delay(300)
return 10
}
suspend fun secondValue(): Int {
delay(250)
throw(ArithmeticException("error occurred"))
}
//console output
got cancelled
Exception in thread "main" java.lang.ArithmeticException: error occurred
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment