简体   繁体   English

将Play Framework java Promise转换为Play Framework Scala Promise

[英]Convert Play Framework java Promise to Play Framework Scala Promise

I am currently building a Scala play framework app which uses a library that return results as F.Promise (Java Promise). 我目前正在构建一个Scala播放框架应用程序,该应用程序使用一个将结果返回为F.Promise(Java Promise)的库。 Is there a way to convert F.Promises ( https://www.playframework.com/documentation/2.1.0/api/java/play/libs/F.Promise.html ) into Scala Promises or get the wrapped Scala Future out of the F.Promise? 有没有一种方法可以将F.Promises( https://www.playframework.com/documentation/2.1.0/api/java/play/libs/F.Promise.html )转换为Scala Promises或将包装好的Scala Future打包出来F.Promise?

The only way I see so far is getting the F.Promise but that is a blocking operation and I would like to continue working asynchronous. 到目前为止,我看到的唯一方法是获取F.Promise,但这是一项阻塞操作,我想继续异步工作。

The way descriped in the first answer led me to this code. 第一个答案中描述的方式将我引导至此代码。 Unfortunately I dont know how to define this F.Function correctly. 不幸的是,我不知道如何正确定义此F.Function。 The code does not compile. 该代码无法编译。

Answer: So, I finally found out that the F.Promise has a method called wrapped(). 答:因此,我终于发现F.Promise有一个称为wrapd()的方法。 And this method gives you a Scala Future back. 而且这种方法为您提供了Scala Future。

It turns out that the class F.Promise (java.play) has a method called wrapped() which returns the scala.concurrente.Future that is wrapped by the Promise. 事实证明,在类F.Promise(java.play)有一个称为方法wrapped()它返回scala.concurrente.Future由无极缠绕。 So all you have to do is calling wrapped on the F.Promise. 因此,您要做的就是调用F.Promise上的包装。

val promise: F.Promise[T] = getPromise()
val future : Future[T] = promise.wrapped()

Well, since there is no 'standard' for promises or futures in that ecosystem, you'd have to do it manually. 好吧,由于该生态系统中没有关于承诺或期货的“标准”,因此您必须手动进行。 There is no concept of 'assimilation' between play promises and scala futures. 游戏承诺和斯卡拉期货之间没有“同化”的概念。

Basically, we treat a F.Promise like we'd treat a callback, we convert it to a scala Future by creating a Promise (which, confusingly in scala means "that which creates a Future"). 基本上,我们像对待回调一样对待F.Promise ,我们通过创建Promise(在scala中令人困惑地表示“创建Future的东西”)将其转换为scala Future。 Here is a description of how this works rather than code since I think that would explain the rationale better: 这里描述了它的工作方式而不是代码,因为我认为这将更好地解释其原理:

  • Create a new Promise[T] - p 创建一个新的Promise[T] -p
  • Call .map on the F.Promise you have, in the F.Function passed to it, call p success with the value of the promise - effectively resolving the promise. 在传递给它的F.Function ,在F.Function上调用.map ,以F.Promise的值调用p success -有效地解决了F.Function
  • Similarly - call .recover on the promise and inside its handler call p failure with the failure reason. 类似地-在promise上调用.recover并在其处理程序内部调用p failure和失败原因。
  • Return p.future , which is a future representing the value p is resolved with - since we mirrored both the success and failure channels this will work. 返回p.future ,这是一个表示p的值可以解决的未来-因为我们镜像了成功和失败的渠道,这将起作用。

This is not unlike how you would convert anything else to a scala Future. 这与您将其他任何东西转换为scala Future的方式没有什么不同。 Converting in the other direction works similarly. 向另一个方向转换的工作方式与此类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM