Skip to content

Instantly share code, notes, and snippets.

@RReverser
Last active June 24, 2020 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RReverser/0c0293589eb25a35105b57e310635b02 to your computer and use it in GitHub Desktop.
Save RReverser/0c0293589eb25a35105b57e310635b02 to your computer and use it in GitHub Desktop.
Embind + Asyncify
# Install tip-of-tree Emscripten
$ emsdk install tot
$ emsdk activate tot
# Build with Embind and Asyncify enabled
$ emcc --bind -s ASYNCIFY mylib.cpp -o mylib.html
#include <string>
#include <iostream>
#include <emscripten/bind.h>
using namespace std::string_literals;
using emscripten::val;
static const val fetch = val::global("fetch");
int main() {
val response = fetch("https://httpbin.org/headers"s).await();
if (response["ok"].as<bool>()) {
auto text = response.call<val>("text").await().as<std::string>();
std::cout << "Response:" << std::endl << text << std::endl;
} else {
std::cerr << "Error " << response["status"].as<int>() << ": "
<< response["statusText"].as<std::string>() << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment