The word “coroutine” is composed of two words: “co” (cooperative) and “routines” (functions).
Coroutines are functions that are cooperative.
Usually, when a function calls a second function, the first cannot continue until the second function finishes and returns to where it was called.
The control remains with the second function until it executes completely and only then can the control return to the first one.
Coroutines are functions where the control is transferred from one function to the other function in a way that the exit point from the first function and the entry point to the second function are remembered – without changing the context.
Thus, each function remembers where it left the execution and where it should resume from.
Using this concept, you can pass around values between functions in the middle of the execution without breaking away the context of each function.
Coroutines are cheaper than threads and hence, enable us to execute concurrent code without much effort.
Free Resources