Last active
October 26, 2018 10:28
-
-
Save mayojava/7a48acf5922e4371812ebe641e4a2f5d to your computer and use it in GitHub Desktop.
Custom Coroutine Scope
This file contains hidden or 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
| 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