Skip to content

Instantly share code, notes, and snippets.

@sys1yagi
Last active June 23, 2017 07:01
Show Gist options
  • Save sys1yagi/7446fd86381a4d0accca19bac627ce47 to your computer and use it in GitHub Desktop.
Save sys1yagi/7446fd86381a4d0accca19bac627ce47 to your computer and use it in GitHub Desktop.
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