I need to execute 4 parallel requests. Here is my code:
suspend fun fetchAsyncData() = coroutineScope {
val first = async { repository.taskFirst() }
val second = async { repository.taskSecond() }
val third = async { repository.taskThird() }
val fourth = async { repository.taskFourth() }
val firstResult = first.await()
val secondResult = second.await()
val thirdResult = third.await()
val fourthResult = fourth.await()
}
The problem is that with this approach, requests are executed in parallel, but I can get answers at the same time. That is, if some of the requests will be executed for 45 seconds, and some for 3 seconds, then I will be able to process the results of my requests only after 45 seconds. My task is that as soon as the answer to the first request is received, pass it to the view model, so that it, in turn, can display this piece of data in a fragment. Further, as soon as another response to the request is received, transfer one more data, and so on.
How can this be done, please help me?