Hello) I have some concern ```private fun loadData...
# coroutines
j
Hello) I have some concern
Copy code
private fun loadData() = with(presenterScope) {
    listOf(
        async {
             metadata = transferOtherBank.getMetadata()
        },
        async {
            sources = transferOtherBank.getSourceList()
        },
        async {
            favorites = transferOtherBank.getFavorites()
        },
        async {
            fee = transferOtherBank.getTransactionFee()
        },
        async {
            banks = transferOtherBank.getBanks()
        }
    )
}
where presenterScope is Coroutine scope provided by moxy2 library (I can attach implementation if it’s necessary)
Copy code
presenterScope.launch {
    loadData().awaitAll()
    setName(favorites, banks)

    Timber.d("ASSSAS after await")

}
Once some of async fails (getFavorites for example) setName(…) function is never called. How can I ensure calling of setName(…) despite any error in any async? Thanks!
d
Just like you would in regular (synchronous) code, that's the beauty of suspending functions:
Copy code
try {
   loadData().awaitAll()
} finally {
    // this will run no matter what
}
You could even add a
catch
block to catch any exceptions.
l
You should replace
with(presenterScope) { }
with
coroutineScope { }
for proper errors propagation.
m
how do you know his scope doesn’t properly handle errors propagation? He didn’t ask that 😛
l
Because it's a common error people make, using external scope for
async
, and since testing for non golden/happy path is not very popular yet, they don't notice until it's in production. He'll tell if I was wrong.
m
if setName doesn’t get called, it means the scope is doing what is supposed to. Nothing wrong with that. ‘It’s common error people make’ - It may as well be. What does that have to do with this thread tho ?
😒 1