Skip to content

Instantly share code, notes, and snippets.

@eleinadani
Created May 11, 2020 15:36
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 eleinadani/b8aa29396f22bb2cb4ff19b8ee4c89f8 to your computer and use it in GitHub Desktop.
Save eleinadani/b8aa29396f22bb2cb4ff19b8ee4c89f8 to your computer and use it in GitHub Desktop.
// Expose a JS asynchronous function to Java
Value asyncFunction = context.eval("js", "(async function(x) { return x; })");
// Invoke the function. The result is a JavaScript Promise object
Value jsPromise = asyncFunction.execute(42);
// Register promise reactions
jsPromise.invokeMember("then", (Consumer<Object>) (result) -> {
System.out.println("JavaScript resolves: " + result);
})
.invokeMember("catch", (Consumer<Throwable>) (error) -> {
System.out.println("JavaScript rejects: " + error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment