You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add EM_ASYNC_JS for native async function support #9709
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:
Being able to integrate more easily with native async APIs.
Consistent synchronous-looking code on both sides of Asyncify.
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");letresponse=awaitfetch("a.html");out("got the fetch response");// (normally you would do something with the fetch here)});
Reacted by Louis DeScioli, Dannii Willis, Gurwinder Singh, Tom Ballinger, Connor Clark and v1rtl
@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.
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.
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".
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.
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.
Activity
kripken commentedon Oct 25, 2019
Nice idea!
cc @jgravelle-google
jgravelle-google commentedon Oct 28, 2019
To clarify, the expectation here would be that this C++
would produce JS functions:
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*"
RReverser commentedon Oct 28, 2019
@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.handleSleepin promise handler.)I don't have a strong preference about the name, although
EM_ASYNC_JSfeels a bit more readable / consistent with output.jgravelle-google commentedon Oct 28, 2019
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.
RReverser commentedon Oct 28, 2019
@jgravelle-google Sounds right to me!
RReverser commentedon Oct 31, 2019
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 functionin either place and have it "just work".gerald-dotcom commentedon May 15, 2020
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
RReverser commentedon May 16, 2020
I don't think it's that old 😅 But yeah, it hasn't had much attention since.
It doesn't wait precisely because
EM_JSgenerates regular JS functions (essentiallyfunction foo() { ... }), butawaitrequires 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.
stale commentedon Jun 2, 2021
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.
RReverser commentedon Jun 2, 2021
Bump.
Add EM_ASYNC_JS macro
Add EM_ASYNC_JS macro
Add EM_ASYNC_JS macro
Add EM_ASYNC_JS macro
Add EM_ASYNC_JS macro (#14728)