Skip to content

Instantly share code, notes, and snippets.

@soudmaijer
Last active April 28, 2021 08:59
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/20e890883283ca2e07e1af6923771c19 to your computer and use it in GitHub Desktop.
Save soudmaijer/20e890883283ca2e07e1af6923771c19 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
fun main() {
lateinit var a: Deferred<Nothing>
lateinit var b: Deferred<Unit>
runBlocking { // Parent scope for both calls. When a fails, both coroutines get cancelled.
a = async { throw RuntimeException() }
b = async { delay(2000).also { println("I leaked!") } } // Will be printed!
}
println(a)
println(b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment