https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

james

03/12/2021, 1:43 PM
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

diesieben07

03/12/2021, 1:45 PM
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

louiscad

03/12/2021, 1:48 PM
You should replace
with(presenterScope) { }
with
coroutineScope { }
for proper errors propagation.
m

myanmarking

03/15/2021, 10:14 AM
how do you know his scope doesn’t properly handle errors propagation? He didn’t ask that 😛
l

louiscad

03/15/2021, 1:02 PM
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

myanmarking

03/15/2021, 1:10 PM
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
3 Views