skip to Main Content

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


  1. 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.

    async function foo() {
        console.log("foo");
    }
    foo(); // logs "foo" as expected
    

    But because of the way Python coroutines are implemented they have to be awaited using asyncio or from another async function.

    async def foo():
        print("foo")
    
    foo()
    # you get:
    #
    # Warning (from warnings module):
    #   File "test.py", line 4
    #     foo()
    # RuntimeWarning: coroutine 'foo' was never awaited
    #
    # and no "foo" prints out
    
    Login or Signup to reply.
  2. 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.

    in some languages, that distinction is a bit blurry.

    If the only coroutines the language supports are async/await ones then users likely can conflate the two concepts.

    coroutines are not necessarily asynchronous

    Coroutines have nothing to do with asynchronous. Asynchronous (async/await) requires* coroutines to work.

    *: T&C apply

    if the consensus is that they are just synonyms, then perhaps one
    should be made a synonym of the other (or just removed)?

    The two terms are not synonymous. As such the coroutine tag shouldn’t be removed because of such a train of thought.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search