Is there a cleaner way to do this in Coroutines, p...
# android
l
Is there a cleaner way to do this in Coroutines, possibly using
flatmap()
or some functional operator, without having to introduce Kotlin Flows?
Copy code
viewModelScope.launch {
  val api = NetworkService().api
  val ordersResponse = api.fetchOrdersCoroutine()
  val deliveryItems = mutableListOf<DeliveryItem>()
  ordersResponse.orders.forEach { orderId ->
    val orderResponse = api.fetchOrderByIdCoroutine(orderId)
    deliveryItems.addAll(orderResponse.items)
  }
}
m
map { }
would work here. Doesn't really matter that its in a coroutine
Copy code
val api = NetworkService().api

val deliveryItems = api.fetchOrdersCoroutine().orders.map { order ->
    api.fetchOrderByIdCoroutine(order.id)
}
l
thanks. this is helpful
🚀 2