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

oday

08/15/2022, 6:47 PM
so I have this piece of code:
Copy code
if (marketingConsentTypes != null && marketingConsentTypes.all { it in validEnumValues }) {
    viewModelScope.launch(coroutineDispatchers.main) {
        marketingConsentTypes.forEach { consentType ->
            giveConsent.execute(
                GiveConsent.Input(
                    consentType = GiveConsent.ConsentType.valueOf(consentType),
                    hasGivenConsent = true
                )
            ).await()
        }

        isLoading.postValue(false)
    }
} else {
    error.postValue(Event(Throwable()))
}
the issue is not that I am running this usecase in a loop, that’s what I want the issue is that I would like to get back the Throwable that this usecase already does return for me, with Rx usually I’d get it back in the error block of the subscribe, here where can I get it to let my mutable property know that the error happened?
oh I can just…doOnError instead of await() immediately…
o

oday

08/18/2022, 1:30 PM
yes I’m looking at that
I am not sure how to go from what i have to the above
Copy code
viewModelScope.launch(coroutineDispatchers.main) {
    suspendCancellableCoroutine<GiveConsent.ConsentType> {
        marketingConsentTypes.forEach { consentType ->

            giveConsent.execute(
                GiveConsent.Input(
                    consentType = GiveConsent.ConsentType.valueOf(consentType),
                    hasGivenConsent = true
                )
            ).doOnError {
                cancel("Message", Throwable("Exception thrown"))
            }
        }
        
        isLoading.postValue(false)

        addSuccessComponents()
    }
}
3 Views