Skip to content

Instantly share code, notes, and snippets.

@Atternatt
Created April 16, 2021 22:32
Show Gist options
  • Save Atternatt/2fa6fdd09a8681386496be52acf9ead1 to your computer and use it in GitHub Desktop.
Save Atternatt/2fa6fdd09a8681386496be52acf9ead1 to your computer and use it in GitHub Desktop.
Combination of async coroutine and Arrows Either object
override suspend fun execute(query: PostsQuery): Either<Failure, List<Post>> {
val operation = if (query.forceRefresh) MainSyncOperation else CacheSyncOperation
return either {
val posts = !repository.getAll(query = query, operation = operation)
posts
.filter { it.featuredImage != null } //we just want posts with featured images
.map { post ->
withContext(coroutineDispatcher) {
async {
val host: String? = try {
URI.create(post.authorUrl).host
} catch (e: IllegalArgumentException) {
null
}
if (host.isNullOrBlank()) {
post
} else {
post.copy(
numberOfSubscribers = !getsubsCountUseCase(
SubscribersQuery(host)
).handleError { 0L })
}
}
}
}.awaitAll()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment