39

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.

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.

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

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

DarkCygnus
  • 7,420
  • 4
  • 36
  • 59
Petteri H
  • 11,779
  • 12
  • 64
  • 94
  • Don't have details, but the easiest options is to make the promise on JS side from functions. For example the q promise library has: q.promise(function(resolve, reject) { ...}) – Jencel Dec 21 '15 at 00:32
  • 3
    Have you looked at how Scala.js handles and converts Scala Futures to JS promises? – Reid Spencer Jan 25 '16 at 04:04
  • I think the Play Framework has a library for this. play.lib.F.Promise has a method 'wrap' that creates a Promise wrapped in a Future, and 'wrapped' which returns the Promise wrapped in a Future. – Chronos Apr 01 '16 at 09:29

1 Answers1

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:

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:

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:

+-----------------------------------+------------------------+-------------------------------------------------------------------------------------------------------+
|              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.              |
+-----------------------------------+------------------------+-------------------------------------------------------------------------------------------------------+
eikooc
  • 2,363
  • 28
  • 37