Skip to content

Add EM_ASYNC_JS for native async function support #9709

Closed

Description

@RReverser

While Asyncify.handleSleep is relatively easy to use, async functions and Promises are pretty ubiquitous in Web APIs, it seems useful to have something more direct.

Why

Adding a built-in macro for async functions would have several benefits:

  1. Being able to integrate more easily with native async APIs.
  2. Consistent synchronous-looking code on both sides of Asyncify.
  3. Easier error handling - right now any async errors will be just reported to browser console, while code remains sleeping, but async functions catch any asynchronously thrown errors and report as promise rejections, which makes it easier to propagate them back to C++ and handle as any other FFI errors.

Example

Before

EM_JS(void, do_fetch, (), {
  Asyncify.handleSleep(function(wakeUp) {
    out("waiting for a fetch");
    fetch("a.html").then(response => {
      out("got the fetch response");
      // (normally you would do something with the fetch here)
      wakeUp();
    });
  });
});

After

EM_ASYNC_JS(void, do_fetch, (), {
  out("waiting for a fetch");
  let response = await fetch("a.html");
  out("got the fetch response");
  // (normally you would do something with the fetch here)
});

Activity

  1. kripken commented on Oct 25, 2019

    @kripken
    Member

    Nice idea!

    cc @jgravelle-google

  2. jgravelle-google commented on Oct 28, 2019

    @jgravelle-google
    Contributor

    To clarify, the expectation here would be that this C++

    EM_JS(void, foo, (), {
      out("hello");
    });
    EM_ASYNC_JS(void, bar, (), {
      let response = await fetch("a.html");
      out(response);
    });

    would produce JS functions:

    function foo() {
      out("hello");
    }
    async function bar () {
      let response = await fetch("a.html");
      out(response);
    }

    Sgtm! Might even be simple, I'll give it a shot.

    Nit: how about EM_JS_ASYNC instead? Keeps all the related functionality greppable with "EM_JS*"

  3. RReverser commented on Oct 28, 2019

    @RReverser
    CollaboratorAuthor

    @jgravelle-google This is correct, assuming that you left sleep handling out of scope of generated example. (You still need to invoke or reuse logic of Asyncify.handleSleep in promise handler.)

    I don't have a strong preference about the name, although EM_ASYNC_JS feels a bit more readable / consistent with output.

  4. jgravelle-google commented on Oct 28, 2019

    @jgravelle-google
    Contributor

    Right, those weren't meant to be equivalent to each other, but rather to show that the generated JS is just literally what the user wrote, and that adding ASYNC in EM_JS is the same as adding async to a JS function.

    Which is to say we don't reason about the JS function body at all. In general I don't think we do anything with ES6 features, but this would be entirely user-driven and so totally opt-in, and not need global changes to our JS glue to support it.

  5. RReverser commented on Oct 28, 2019

    @RReverser
    CollaboratorAuthor

    @jgravelle-google Sounds right to me!

  6. RReverser commented on Oct 31, 2019

    @RReverser
    CollaboratorAuthor

    Side-note: when adding async sleep handling for these functions, it should easily cover not only to EM_ASYNC_JS, but also to those in JS libraries so that developers can put async function in either place and have it "just work".

  7. gerald-dotcom commented on May 15, 2020

    @gerald-dotcom

    Sorry for jumping into this old topic. Is there are any way to use Await keyword in EM_JS at this stage?

    I was able to make Async function but it seems mystery why the code not wait for Await keyword

  8. RReverser commented on May 16, 2020

    @RReverser
    CollaboratorAuthor

    I don't think it's that old 😅 But yeah, it hasn't had much attention since.

    I was able to make Async function but it seems mystery why the code not wait for Await keyword

    It doesn't wait precisely because EM_JS generates regular JS functions (essentially function foo() { ... }), but await requires async JS functions (async function foo() { ... }).

    This issue is precisely about that - ability to generate async functions and use their resulting Promise. I don't know if @jgravelle-google has any time on his plate to implement this though.

  9. stale commented on Jun 2, 2021

    @stale

    This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.

  10. RReverser commented on Jun 2, 2021

    @RReverser
    CollaboratorAuthor

    Bump.

  11. added 4 commits that reference this issue on Jul 22, 2021
    f66174f
    199ee54
    e00d028
    d877caa
  12. added a commit that references this issue on Jul 26, 2021
    e77e71c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions