Simon Lin
12/14/2021, 6:34 AMfun foo() {
viewModelScope.launch {
val inputList = listOf("a", "b", "c")
val deferredList = mutableListOf<Deferred<String>>()
repeat(3) { index ->
deferredList += async {
delay((1L..10000L).random())
inputList[index]
}
}
val outputList = deferredList.map { it.await() } // a, b, c
}
}
Tijl
12/14/2021, 7:05 AMawaitAll
for your deferredList
though in this case it’s mostly a syntactic difference. In a more complex example it will be different (it will fail fast).Tijl
12/14/2021, 7:05 AMbuildList
Nick Allen
12/14/2021, 4:48 PMinputList.map { item -> async { doSuspendStuff(item) } }.awaitAll()