Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Coroutines are computer program components that generalize subroutines to allow multiple entry points for suspending and resuming execution at certain locations.
"Subroutines are special cases of ... coroutines." –Donald Knuth.
When subroutines are invoked, execution begins at the start, and once a subroutine exits, it is finished;
an instance of a subroutine only returns once, and does not hold state between invocations.
By contrast, coroutines can exit by calling other coroutines,
which may later return to the point where they were invoked in the original coroutine;
from the coroutine's point of view, it is not exiting but calling another coroutine.
Thus, a coroutine instance holds state, and varies between invocations;
there can be multiple instances of a given coroutine at once.
The difference between calling another coroutine by means of "yielding" to it
and simply calling another routine (which then, also, would return to the original point),
is that the latter is entered in the same continuous manner as the former.
The relation between two coroutines which yield to each other is not that of caller-callee, but instead symmetric.
-- Wikipedia
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment