Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
fun <T> async(context: CoroutineContext = CommonPool, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T)
= kotlinx.coroutines.experimental.async(context, start, block)
fun ui(start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit)
= launch(UI, start, block)
suspend fun <T, U> parallel(c1: suspend CoroutineScope.() -> T, c2: suspend CoroutineScope.() -> U): Pair<T, U> {
val c1Job = async(block = c1)
val c2Job = async(block = c2)
return Pair(c1Job.await(), c2Job.await())
}
suspend fun <T, U, V> parallel(c1: suspend CoroutineScope.() -> T, c2: suspend CoroutineScope.() -> U, c3: suspend CoroutineScope.() -> V): Triple<T, U, V> {
val c1Job = async(block = c1)
val c2Job = async(block = c2)
val c3Job = async(block = c3)
return Triple(c1Job.await(), c2Job.await(), c3Job.await())
}
val (user, article) = parallel(
{ usersApi.me() },
{ articlesApi.getPublicTimeline() }
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment