Skip to content

Instantly share code, notes, and snippets.

@tomkoptel
Created April 14, 2018 11:06
Show Gist options
  • Select an option

  • Save tomkoptel/c5ebd5cbc1fdba524db4bc2eae0d3214 to your computer and use it in GitHub Desktop.

Select an option

Save tomkoptel/c5ebd5cbc1fdba524db4bc2eae0d3214 to your computer and use it in GitHub Desktop.
Example of launching 3 network requests in on coroutine block. Couroutine get created by artificial wrapper factory API.
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
class Model(private val coroutines: CoroutinesProvider) : ViewModel() {
fun load() {
coroutines.launch(UI) {
// Start 3 calls in parallel
val response1 = api.makeAcall()
val response2 = api.makeAcall()
val response3 = api.makeAcall()
// Wait for result and offload work to background thread
val result = async {
combine(response1.await(), response2.await(), response3.await())
}.await()
render(result)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment