Yes, that's currently the best way to handle that situation and what embind 
currently does 
<https://github.com/emscripten-core/emscripten/blob/bea49408e63a7f0fe6279e9ce5016a6091535eea/src/embind/embind.js#L860-L863>
. 

As for it being complex, yes. This was an optimization so that functions 
that don't need to suspend don't have to return the micro task queue. It 
also made it so code that would only trigger the sync behaviour didn't need 
to be re-written to handle the async version.

FWIW, with JSPI (what will hopefully replace asyncify) async exports will 
always return a promise. We've been discussing changing asyncify to match 
this behavior, but there are downsides as mentioned above. 

On Tuesday, October 24, 2023 at 9:39:33 PM UTC-7 [email protected] wrote:

> First time looking into Asyncify again. Impressed with the improvements 
> since I last looked into it!
>
> One question: I am calling some exported top-level function that may or 
> may not call some nested asynchronous function. If it does, then waiting on 
> the promise returned by `Asyncify.whenDone()` gives me the function's 
> result. If the top-level function does not call the nested asynchronous 
> function, then its result is synchronously returned. 
>
> The only way for me to know whether or not I need to wait on 
> `Asyncify.whenDone()` is to check whether `Asyncify.currData === null`. 
>
> Is this how it is supposed to be? So something like:
>
> ```
> async function callExportedFunction() {
>    const syncResult = Module.topLevelFunction();
>
>     if (Asyncify.currData === null) return syncResult;
>
>      return Asyncify.whenDone();
> }
> ```
>
> This works, but seems inconsistent and unnecessarily complex. Could the 
> exported function always directly return a promise? Or at least, can 
> `Asyncify.whenDone()` resolve immediately when the result is synchronously 
> available?
>
> Soeren
>

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/emscripten-discuss/85264e3f-7ee9-42b5-ab7c-3179b0cdf1e2n%40googlegroups.com.

Reply via email to