Fetching the data concurrently

Now we have everything we need in order to request many feeds concurrently. The idea now is to create one deferred for each feed on the list. Let's first update the asyncLoadNews() function so that it has a list where we can keep track of every deferred that we will be waiting for:

private fun asyncLoadNews() = launch {
val requests = mutableListOf<Deferred<List<String>>>()
...

}

Then, let's add an element to the list for each feed on the list:

val requests = mutableListOf<Deferred<List<String>>>()

feeds.mapTo(requests) {
asyncFetchHeadlines(it, dispatcher)
}

Following that, let's add code to wait for each of them to complete:

requests.forEach {
it.await()
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.157.34