In JavaScript we always had promises. Then async/await came into the language, which I thought was a wrapper around promises. Then generators came into the language with yield
keyword, which started to look very much like coroutines.
This answer states that:
Coroutines and/or generators can be used to implement cooperative functions. Instead of being run on kernel threads and scheduled by the operating system, they run in a single thread until they yield or finish, yielding to other functions as determined by the programmer. Languages with generators, such as Python and ECMAScript 6, can be used to build coroutines. Async/await (seen in C#, Python, ECMAscript 7, Rust) is an abstraction built on top of generator functions that yield futures/promises.
It talks about both async/await and generators as a wrapper for coroutines implementation. This seems to be the case in Python, but is it so now in JavaScript as well?
2
Answers
As far as I can tell, JavaScript async/await is still a wrapper around promises. They may be implemented now with generators but they still work like promises.
If you don’t await a JavaScript async function, it still runs.
But because of the way Python coroutines are implemented they have to be awaited using asyncio or from another async function.
Coroutines are a control flow mechanism. You can reimplement coroutines by using goto and mutating an address both sides have access to. As such a coroutine is to goto, what for loops are to while loops. A form of sugar.
Async/await requires arbitrary suspension and resumption of functions so builds off of coroutines. As goto would be horrible to work with. However the async/await pattern specializes coroutines with a scheduler to manage the coroutines.
You can build your own async/await scheduler in any language which supports coroutines (not generators, which only support unidirectional communication). Which is how Python had support for the async/await pattern way before async and await were added to the language.
If the only coroutines the language supports are async/await ones then users likely can conflate the two concepts.
Coroutines have nothing to do with asynchronous. Asynchronous (async/await) requires* coroutines to work.
*: T&C apply
The two terms are not synonymous. As such the coroutine tag shouldn’t be removed because of such a train of thought.