hello, I got a prototype bean that is created at r...
# coroutines
hello, I got a prototype bean that is created at runtime based on some input parameters. Within the prototype bean I got multiple methods that accept value (and some others that dont) that should be computed just once (as it is pretty expensive). Since I don't want to incur the initialization penalty if I'm not going to be calling a method that needs it I was using
by lazy
. I'm migrating now to coroutines was wondering whats the proper way to update
by lazy
to call suspendable function. Any thoughts on how to make this better?
I’m also interested in this. Did you find out anything? Maybe of interest: https://github.com/Kotlin/kotlinx.coroutines/issues/745
lazy delegation doesn't fit well into coroutine world
simplest equivalent is to just create lazy deferred, i.e.
async(start = CoroutineStart.LAZY)
and just await on it from the places
with the example above it would simply become
Copy code
private val myDeferred = GlobalScope.async(start = CoroutineStart.LAZY) {
    suspend fun functionA() = myAnotherService.firstMethod(myDeferred.await())
    suspend fun functionB() = myAnotherService.secondMethod(myDeferred.await())
👍 1
*noted you probably want to create your own
instead of a global one