码迷,mamicode.com
首页 > Web开发 > 详细

使用graalvm.js调用promise

时间:2019-02-09 13:14:53      阅读:401      评论:0      收藏:0      [点我收藏+]

标签:string   sum   sys   end   access   exe   consumer   eject   print   

前提

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

标签:string   sum   sys   end   access   exe   consumer   eject   print   

原文地址:https://www.cnblogs.com/tangyouwei/p/10357340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!