Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.