Hacker News new | ask | show | jobs
by marhee 635 days ago
Coroutines are just multiple call stacks. If coroutine A calls coroutine B then B excutes on its on stack and can ‘yield’ a value back to A. Yielding is just a return across stacks without destroying the current stack. So A continued with the yielded valie on its own stack and when ready calls B again which continues on its own stack with the next statement after the previous yield. Etc.

Notice that this does not necessarily involve parallelism, although it can. For example, Lua has non parallel (cooperative) co-routines. Go had parallel coroutins, called goroutines, but theoretically only if they they use channels to exchange values. Otherwise, if they’re not exchanging information they would not becoroutins in the sense that they work together in solving something.