Florent Dambreville
1.6.20
suspendCoroutine
var topupCountries: Deferred<Set<String>> = buildCountriesAsync() private set private fun buildCountriesAsync() = CoroutineScope(backgroundDispatcher).async(start = CoroutineStart.LAZY) { getCountries() } private suspend fun getCountries(): Set<String> { return try { topupCountries.getCompleted() } catch (exception: IllegalStateException) { // Crash here fetchRestrictedTopupCountries() } } private suspend fun fetchRestrictedTopupCountries(): Set<String> { return suspendCoroutine { continuation -> val request = ApiTopupRequestFactory.getTopupCountries( listener = { val setResult = it.countries.toSet() continuation.resume(setResult) }, errorListener = { continuation.resume(setOf()) } ) execute(request) } }
java.lang.ClassCastException: kotlin.coroutines.intrinsics.CoroutineSingletons cannot be cast to java.util.Set
inline
crossinline
suspend inline fun <T> suspendCoroutine(
crossinline block: (Continuation<T>) -> Unit
): T
getCompleted
A modern programming language that makes developers happier.