-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return response from asynchronous fetch call made inside C++ function bound using Embind #18834
Comments
We have recently add a C and C++ interface to JS promises. See #18598. However, making this work with embind would likely require some kind of magic such that the actual JS promise is returned rather then integer handle. @tlively @brendandahl any ideas? |
@tlively I guess step one would be some kind of std::future and promise.h integration? i.e. being able to create a promise based on a std::future? |
I guess in summary, I am looking for a way to allow JS to await an asynchronous call that was started in C++, namely My solution right now is to make a synchronous call to |
I get the following warning with the above code:
I understand that |
We have APIs for proxying other work to background threads though. See |
For my use, I found it easier to fetch using "inline" JS as opposed to #include <emscripten/bind.h>
#include <emscripten/emscripten.h>
EM_ASYNC_JS(int, do_fetch, (), {
let response = await fetch("https://example.com/");
return await response.status;
});
int foo() {
return do_fetch();
}
EMSCRIPTEN_BINDINGS(my_module) {
emscripten::function("foo", &foo);
} This returns a promise when using |
Although it may be possible to integrate with I don't know enough about embind to say what would be required to make it work nicely with promise.h. Once we have a C++ API for promises, let's discuss this again and figure something nice out. |
I am writing a function in C++ that uses the Emscripten Fetch API. This function is invoked from JavaScript using Embind. I would like to return the response of an asynchronous Fetch call back to JS. Ideally, I would return a promise, as is the case when using Asyncify. However, according to #12255, Fetch is not compatible with Asyncify.
Here is an attempt at a solution, but it does not return a promise like Asyncify would:
What would be the best way to accomplish this? Any help would be greatly appreciated. Thanks!
The text was updated successfully, but these errors were encountered: