Resolve a promise using emscripten::val

77 views
Skip to first unread message

Mehdi Sabwat

unread,
Nov 25, 2019, 1:03:05 PM11/25/19
to emscripten-discuss
Hi,

I have been looking for a way to resolve a Promise using emscripten::val, is that possible? if not, would it be possible in  C/C++ without wrapping it in an EM_ASM?

I have been thinking about Atomics.wait, but I am not sure it is the best way because it is limited to Int32Array. For now, I am using emscripten_async_call(), it would be great if I could replace it with something less hacky.

Thanks

Mehdi

Brion Vibber

unread,
Nov 25, 2019, 2:03:41 PM11/25/19
to emscripten Mailing List
Are you trying to create a C++ function that schedules work asynchronously and synchronously returns a Promise which can be used by JS code to wait on and retrieve a value?

This should be possible through emscripten::val by translating the equivalent code from JS to val invocations, though I've not tested it... You'd still need to use emscripten_async_call unless there's some other way you intend to schedule the work to happen after the call, such as manual use of setTimeout via val or some other callback.

-- brion

--
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 emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/978f8d00-0cf1-4dec-bbe5-b9c7b20a1a61%40googlegroups.com.

Mehdi Sabwat

unread,
Nov 26, 2019, 10:42:13 AM11/26/19
to emscripten-discuss
I have a JS call that returns a promise. for example :

createAudioFileAsync(audioSettings).then(successCallback);

I don't want to use emscripten_async_call because I don't know when the promise will be resolved.

I tried to val.call<>("then", ...); but I am running into a clang bug.

On Monday, November 25, 2019 at 8:03:41 PM UTC+1, Brion Vibber wrote:
Are you trying to create a C++ function that schedules work asynchronously and synchronously returns a Promise which can be used by JS code to wait on and retrieve a value?

This should be possible through emscripten::val by translating the equivalent code from JS to val invocations, though I've not tested it... You'd still need to use emscripten_async_call unless there's some other way you intend to schedule the work to happen after the call, such as manual use of setTimeout via val or some other callback.

-- brion

On Mon, Nov 25, 2019 at 10:03 AM Mehdi Sabwat <mehdi...@gmail.com> wrote:
Hi,

I have been looking for a way to resolve a Promise using emscripten::val, is that possible? if not, would it be possible in  C/C++ without wrapping it in an EM_ASM?

I have been thinking about Atomics.wait, but I am not sure it is the best way because it is limited to Int32Array. For now, I am using emscripten_async_call(), it would be great if I could replace it with something less hacky.

Thanks

Mehdi

--
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 emscripten-discuss+unsub...@googlegroups.com.

Mehdi Sabwat

unread,
Dec 6, 2019, 1:29:21 PM12/6/19
to emscripten-discuss

// I can get result["whatever"], or add parameters, depending on the expected callback
void print(val result)
{
  std
::cout << "Hi there..." << "\n";
}

int main()
{
  val prom
  promise
.call<void>("then", val::module_property("Zecallback"));
 
return 0;
}
EMSCRIPTEN_BINDINGS
(name) {
 
function("Zecallback", &print);
}

couldn't reproduce the bug though, will post it when it pops back.
Reply all
Reply to author
Forward
0 new messages