cdurham
07/10/2025, 6:36 PM// assuming this is a singleton
private val deferredThing: Deferred<Thing> =
ioScope.async(start = CoroutineStart.LAZY) {
createThing()
}
Are there guarantees that this block will only run a single time, no matter how many times it’s awaited / which threads call await?Youssef Shoaib [MOD]
07/10/2025, 6:43 PMDmitry Khalanskiy [JB]
07/11/2025, 12:10 PMprivate val deferredThing: Deferred<Thing> by lazy {
ioScope.async {
createThing()
}
}
This way, if deferredThing
is never used, ioScope
won't even learn about its existence, + it's using the more general mechanism, applicable in more contexts.cdurham
07/11/2025, 3:38 PMYoussef Shoaib [MOD]
07/11/2025, 3:54 PMlazy
or LAZY
thencdurham
07/11/2025, 4:07 PM