次の JS コードがあるとします。
async function helloAsync(){
return "Hello";
}
function hello(){
return "Hello";
}
Java では、以下を使用してこのコードを GraalVM コンテキスト オブジェクトにロードできます。
context.eval("js", mappingTemplate);
以下を使用して評価できる 2 つのメンバーを指定します。
Value bindings = context.getBindings("js");
final Value executionResult1 = bindings.getMember("hello")
.execute();
final Value executionResult2 = bindings.getMember("helloAsync")
.execute();
その結果、これはexecutionResult2
Java 内で完了することができる promise になります。私の質問はexecutionResult2
、executionResult1
. 現在、素朴で信頼性の低いアプローチは次のようになります。
if (executionResult.toString().startsWith("Promise") &&
executionResult.hasMember("then") && executionResult.hasMember("catch"))
JSから返された約束を認識するより信頼できる/エレガントな方法は何ですか?