https://kotlinlang.org logo
Title
d

Dariusz Kuc

04/06/2021, 2:10 PM
Hello 👋 Was just wondering about Flow and context (https://kotlinlang.org/docs/flow.html#flow-context). I understand that we should not be emitting new values from new coroutines but is it fine to start coroutines to fetch some data or that should still fall under
channelFlow
? e.g.
flowOf(1, 2, 3).map { 
  id -> coroutineScope {
    val first = async { retrieve(id) }
    val second = async { retrieveOther(id) }
    first.await() + second.await()
  }
}
l

louiscad

04/06/2021, 2:14 PM
Your snippet correct. That rule only applies to call to
emit
👍 1
d

Dariusz Kuc

04/06/2021, 2:16 PM
thanks
l

louiscad

04/06/2021, 2:20 PM
The reason is that calling
emit
calls code from the collector side directly, and you'd not expect it to be cancelled by the upstream flow code or have it run on an unknown dispatcher.
In other words, the
emit
function calls the lambda of
collect
.
d

Dariusz Kuc

04/06/2021, 2:37 PM
Thanks for the explanation