Nashorn和Scala将来的JS Promise转换

Pet*_*rta 39 javascript java scala nashorn

我有一个服务器端实现ScalaReact/Flux基于前端.我的服务返回Futures,它们在Scalatra的AsyncResultJSON响应中处理.

对于同构/服务器端呈现设置,我不想将服务更改为阻塞,所以我开始使用此处显示的 Scala Future-> java.util.function.Function转换.

但是Flux的调度员想拥有JS Promise.到目前为止,我发现这张Slides 68-81只有相当复杂的声音方式

有没有推荐的方法来处理这个Scala Future - > JS Promise转换?

eik*_*ooc 1

我将尝试回答 Scala Future 到 JS Promise 部分问题。因为您没有提供示例。我将在这里提供一个转换。如果我们说我们有一个以 Scala 方式实现的 Future:

val f: Future = Future {
  session.getX()
}

f onComplete {
  case Success(data) => println(data)
  case Failure(t) => println(t.getMessage)
}
Run Code Online (Sandbox Code Playgroud)

那么 JavaScript/ES6 中的相应代码可能如下所示:

var f = new Promise(function(resolve, reject) {  
   session.getX();
});

f.then((data) => {   
  console.log(data);
}).catch((t) => {
  console.log(t);
}));
Run Code Online (Sandbox Code Playgroud)

我知道这不是 Scala,但为了完整性我想将其包括在内。这是取自Scala.jsFuture文档的to的映射:Promise

+-----------------------------------+------------------------+-------------------------------------------------------------------------------------------------------+
|              Future               |        Promise         |                                                 Notes                                                 |
+-----------------------------------+------------------------+-------------------------------------------------------------------------------------------------------+
| foreach(func)                     | then(func)             | Executes func for its side-effects when the future completes.                                         |
| map(func)                         | then(func)             | The result of func is wrapped in a new future.                                                        |
| flatMap(func)                     | then(func)             | func must return a future.                                                                            |
| recover(func)                     | catch(func)            | Handles an error. The result of func is wrapped in a new future.                                      |
| recoverWith(func)                 | catch(func)            | Handles an error. func must return a future.                                                          |
| filter(predicate)                 | N/A                    | Creates a new future by filtering the value of the current future with a predicate.                   |
| zip(that)                         | N/A                    | Zips the values of this and that future, and creates a new future holding the tuple of their results. |
| Future.successful(value)          | Promise.resolve(value) | Returns a successful future containing value                                                          |
| Future.failed(exception)          | Promise.reject(value)  | Returns a failed future containing exception                                                          |
| Future.sequence(iterable)         | Promise.all(iterable)  | Returns a future that completes when all of the futures in the iterable argument have been completed. |
| Future.firstCompletedOf(iterable) | Promise.race(iterable) | Returns a future that completes as soon as one of the futures in the iterable completes.              |
+-----------------------------------+------------------------+-------------------------------------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)