That's a really good explanation! It brings up a bit of a broader question for me though:
I've been trying hard to develop a mental model for coroutines. It seems they are kind of like threads, but not quite, but perhaps executed on a thread 🤷 I'm also having a lot of trouble imagining how they are actually implemented.
Is it safer to imagine coroutines as an event loop, where you can enque new tasks via
launch
? Scopes are different queues? Dispatchers allow you to run queues on different threads? functions like
join
,
yeild
and
delay
alow you to split your function into separate tasks?
Is this an accurate mental model of coroutines? Is this somewhat similar to how they are actually implemented behind the scenes?