bytecodealliance / javy

JS to WebAssembly toolchain

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for async / await

munjalpatel opened this issue · comments

What is your question?

Is it possible to use async/await in JS that will be compiled to wasm using Javy?

Hello @munjalpatel,

Not sure about your use case, but I could use async/await in my JS runtime based on Javy. The key methods are is_pending and execute_pending. They allow you to run any async method that it's waiting.

From the Javy / core crate, the run_bytecode_inner runs pending items. You only need to enable the experimental_event_loop feature:

fn run_bytecode_inner(runtime: &Runtime, bytecode: &[u8]) -> Result<()> {
let context = runtime.context();
context.eval_binary(bytecode)?;
if cfg!(feature = "experimental_event_loop") {
context.execute_pending()?;
} else if context.is_pending() {
bail!("Adding tasks to the event queue is not supported");
}
Ok(())
}

If your use case is related to using Javy as a library to create your own JS runtime, you can use those methods to ensure QuickJS processes the async methods. I recently added this feature to a project I'm working on. You can find the code in this PR: vmware-labs/wasm-workers-server#143.

As mentioned in #386, the CLI does not currently support network requests or the fetch API.

As @Angelmmiguel mentions, you can use the library crates in Javy to create your own runtime that you compile to a Wasm module and build your own CLI around that module.

Closing this for now. Feel free to comment if you feel that your question has not been answered.