使用GWT JsInterop的Javascript Promise对象 [英] Javascript Promise object with GWT JsInterop

查看:81
本文介绍了使用GWT JsInterop的Javascript Promise对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有要用于初始化()第三方Javascript库的以下代码:

I have this code that I want to use to init() a third-party Javascript library:

@JsType(namespace = JsPackage.GLOBAL, name = "Kinvey", isNative = true)
public class Kinvey {
    public String appKey;
    public String appSecret;
    public static native void init();
}

问题是init函数返回Javascript Promise对象, GWT如何使用JsInterop处理该对象?

The problem is the init function returns a Javascript Promise object, how can GWT deal with this object using JsInterop?

var promise = Kinvey.init({
    appKey    : 'your_app_key',
    appSecret : 'your_app_secret'
});
promise.then(function(activeUser) {
    ...
}, function(error) {
    ...
});

推荐答案

此示例用于从GWT代码返回JS承诺,但是您可以对其进行调整,只需将Promise Javascript类及其Java包装起来即可.对应类,同时声明then()catch()方法:

This example is use to return JS promises from GWT code, but you can adapt it for your scenario, it just wraps the Promise Javascript class with its Java counterpart class, declaring also then() and catch() methods:

@JsType(isNative = true, namespace = JsPackage.GLOBAL)
public class Promise {

  @JsFunction
  public interface FunctionParam {
      void exec(Object o);
  }

  @JsFunction
  public interface ConstructorParam {
      void exec(FunctionParam resolve, FunctionParam reject);
  }

  @JsConstructor
  public Promise(ConstructorParam parameters) {
  }

  public Promise then(FunctionParam f) { }

  public Promise catch(FunctionParam f) { }
}

这篇关于使用GWT JsInterop的Javascript Promise对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆