Created
May 11, 2020 15:36
-
-
Save eleinadani/b8aa29396f22bb2cb4ff19b8ee4c89f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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