Skip to content

Instantly share code, notes, and snippets.

@FilipeLipan
Last active March 18, 2020 14:48
Show Gist options
  • Save FilipeLipan/5aec101638cb71782ddd251284d627ca to your computer and use it in GitHub Desktop.
Save FilipeLipan/5aec101638cb71782ddd251284d627ca to your computer and use it in GitHub Desktop.
viewModelScope.launch {
runWithExecutor(
asyncFunction = async { getSupportedCountriesUseCase.execute(UseCase.None()) },
contextExecutor = coroutinesDispatcherProvider.io) {
it.fold(::showError, ::showCountries)
}
}
fun <T> CoroutineScope.runWithExecutor(
asyncFunction: Deferred<Either<Failure, T>>,
contextExecutor: CoroutineContext,
onNext: (Either<Failure, T>) -> Unit = {}) {
this.launch {
val result: Either<Failure, T> = withContext(contextExecutor) {
asyncFunction.await()
}
withContext(Dispatchers.Main) {
onNext(result)
}
}
}
open class CoroutinesDispatcherProvider {
open val main: CoroutineContext by lazy { Dispatchers.Main }
open val io: CoroutineContext by lazy { Dispatchers.IO }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment