I am very confused about the difference between coroutines and suspend functions, and this is what I think about them. Am I correct?
Coroutine is like Threads but lightweight, it basically creates a new lightweight job or task on the other hand, the suspend function is like a function which is signalling that - "O My Coroutine, I might have some task which might take longer so you can run me on your own thread" hence suspend/pause the Coroutines execution.
In Short, a Coroutine is just like a bus and suspend functions are like stops.
k
kevin.cianfarini
09/10/2024, 7:44 PM
A coroutine is the thing that runs suspend functions. Suspend functions signal to the coroutine they’re running in that they are suspended. Your bus stop analogy is fairly good.
b
Bhaskar Singh
09/10/2024, 7:50 PM
Why do we even need a suspend function why not just the whole function be suspended. I mean whatever code we write inside Coroutines why not consider it suspended by default why introduce a new keyword?
🧵 1
k
kevin.cianfarini
09/10/2024, 7:51 PM
Because it’s good to have a distinction between functions which will be fast and functions which might take a long time
kevin.cianfarini
09/10/2024, 7:51 PM
@elizarov has a talk about this on youtube that touches on this in relation to loom
kevin.cianfarini
09/10/2024, 7:51 PM
https://www.youtube.com/watch?v=zluKcazgkV4▾
b
Bhaskar Singh
09/10/2024, 7:53 PM
Thanks, @kevin.cianfarini! I'll watch it right away.