Nashorn和Scala将来的JS Promise转换

时间:2022-09-25 21:09:11

I have a server side implemented in Scala and React/Flux based front end. My services return Futures and they are handled within Scalatra's AsyncResult for JSON responses.

我在Scala和React / Flux的前端实现了服务器端。我的服务返回Futures,它们在Scalatra的AsyncResult中处理JSON响应。

For isomorphic/server side rendering setup I did not want to change services to be blocking so I started with Scala Future-> java.util.function.Function conversion shown here.

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

But the dispatcher in Flux would like to have JS Promise. So far I found only rather complicated sounding way around this Slides 68-81

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

Is there any recommended way to deal with this Scala Future -> JS Promise conversion?

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

1 个解决方案

#1


1  

I will try to answer the Scala Future to JS Promise part of the question. As you haven't provided an example. I will provide one here with the conversion. If we say that we have a Future implemented in Scala this way:

我将尝试回答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)
}

then the corresponding code in JavaScript/ES6 could look like this:

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

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

f.then((data) => {   
  console.log(data);
}).catch((t) => {
  console.log(t);
}));

I know this is not Scala, but I wanted to include it for completeness. This is a mapping of Future to Promise taken from Scala.js docs:

我知道这不是Scala,但我希望将其包含在内以保证完整性。这是从Scala.js docs获取的Future到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.              |
+-----------------------------------+------------------------+-------------------------------------------------------------------------------------------------------+

#1


1  

I will try to answer the Scala Future to JS Promise part of the question. As you haven't provided an example. I will provide one here with the conversion. If we say that we have a Future implemented in Scala this way:

我将尝试回答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)
}

then the corresponding code in JavaScript/ES6 could look like this:

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

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

f.then((data) => {   
  console.log(data);
}).catch((t) => {
  console.log(t);
}));

I know this is not Scala, but I wanted to include it for completeness. This is a mapping of Future to Promise taken from Scala.js docs:

我知道这不是Scala,但我希望将其包含在内以保证完整性。这是从Scala.js docs获取的Future到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.              |
+-----------------------------------+------------------------+-------------------------------------------------------------------------------------------------------+