使用graalvm.js调用promise
Posted 远方的灯塔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用graalvm.js调用promise相关的知识,希望对你有一定的参考价值。
前提
1、JDK1.8
2、引入jar包
<!--graalvm.js -->
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
<version>1.0.0-rc12</version>
</dependency>
运行
package com.terwergreen;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
import java.util.function.Consumer;
public class Main {
private Object renderedObject = null;
private Consumer<Object> fnResolve = object -> {
renderedObject = object;
System.out.println("fnResolve=>promiseResolved");
};
private Consumer<Object> fnRejected = object -> {
renderedObject = object;
System.out.println("fnRejected=>promiseRejected");
};
private void execute() {
try (Context context = Context.newBuilder("js").allowAllAccess(true).build()) {
Value eval = context.eval("js",
"(async function() { return 'Hello World' });"
);
eval.execute().invokeMember("then", fnResolve, fnRejected);
System.out.println("renderedObject = " + renderedObject);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Main main = new Main();
main.execute();
}
}
以上是关于使用graalvm.js调用promise的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段12——JavaScript的Promise对象