Pretty basic question, in cases like this: ```val ...
# coroutines
g
Pretty basic question, in cases like this:
Copy code
val account = async { getAccountByUuid(accountUUID) }
val devices = async { deviceRepository.findDevicesByUuid(accountUUID) }
account.await()?.addDevices(devices.await()) ?: return@coroutineScope null
In case the account.await() is null, the devices.await() is still waiting or it is just canceled ? I believe it is canceled but just want to make it sure. Thanks in advance for any answers !
s
It won’t be cancelled unless you cancel it explicitly. I’m guessing you’re using a
coroutineScope { … }
block here? In that case, even though you don’t reach the call to
devices.await()
, the scope will still wait for all of its children to complete before it returns.
g
yes i do have a coroutineScope, hmm then like this it's ok i suppose:
Copy code
override suspend fun getAccountByUuidWithDevices(accountUUID: UUID): Account? = coroutineScope {
    val account = async { getAccountByUuid(accountUUID) }
    val devices = async { deviceRepository.findDevicesByUuid(accountUUID) }
    val accountResult = account.await()
    if (accountResult == null) {
        devices.cancel()
        return@coroutineScope null
    }
    accountResult.addDevices(devices.await())
}
in that case is it even worth it to cancel it manually rather than just executing sequential ? they are both db calls.
s
I think your approach of cancelling it explicitly is sensible 👍
j
I guess now you can simplify it by removing the
async
for
getAccount
- just call it directly where you currently await the corresponding deferred