Skip to content

Instantly share code, notes, and snippets.

@tomkoptel
Last active April 14, 2018 11:00
Show Gist options
  • Save tomkoptel/2c6609e21c5f4090b7a7e0f3b264bf4a to your computer and use it in GitHub Desktop.
Save tomkoptel/2c6609e21c5f4090b7a7e0f3b264bf4a to your computer and use it in GitHub Desktop.
Example of launching 3 network requests in on coroutine block.
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
class Model : ViewModel() {
fun load() {
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