https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

dimsuz

01/27/2020, 10:24 PM
Hi! What will happen if I call multiple suspend functions from
launch
like so:
Copy code
myScope.launch {
  suspendingFunction1()
  someFlow
    .onEach { suspendingFunction2() }
    .collect { 
      someChannel.send(Unit) // 'send' is also a suspending fun
    }
}
Will each of them block? (inside this coroutine)
o

octylFractal

01/27/2020, 10:27 PM
none of them block, but they will each suspend. I'm not sure what you would expect other wise?
z

Zach Klippenstein (he/him) [MOD]

01/27/2020, 10:40 PM
If they all suspend, then the execution will look like: 1.
suspendingFunction1()
suspends 2.
suspendingFunction1()
resumes 3.
someFlow.onEach
(returns a Flow) 4.
collect
suspends 5.
someFlow
emits 6.
suspendingFunction2()
suspends 7.
suspendingFunction2()
resumes 8.
someChannel.send
suspends 9.
someChannel.send
resumes 10. Repeat 5-9 until
someFlow
completes 11.
collect
resumes
👍 2
d

dimsuz

01/27/2020, 10:55 PM
what you would expect other wise?4
I forgot to mention this might be a silly question, I'm starting to learn this and my understanding hasn't yet formed a good mental model of how everything behaves... zachklipp This is awesome, thank you very much, just what I've needed. I think I might think of each
suspend
function in terms of continuation (which I now recall it actually is). Whenever I see a call to suspend function I may translate everything after a call to it as a continuation to be called when body of said function will complete executing on current Dispatcher.
👍 1
z

Zach Klippenstein (he/him) [MOD]

01/27/2020, 11:57 PM
That’s exactly what I do! Turn everything back into callbacks in my head.
👌 1
5 Views