Ich werde versuchen, die Scala Future zu JS Promise Teil der Frage zu beantworten. Da Sie kein Beispiel angegeben haben. Ich werde hier einen mit der Konvertierung versorgen. Wenn wir sagen, dass wir eine Zukunft umgesetzt in Scala auf diese Weise haben:
var f = new Promise(function(resolve, reject) {
session.getX();
});
f.then((data) => {
console.log(data);
}).catch((t) => {
console.log(t);
}));
Ich weiß, dass dies nicht Scala, aber:
val f: Future = Future {
session.getX()
}
f onComplete {
case Success(data) => println(data)
case Failure(t) => println(t.getMessage)
}
dann der entsprechende Code in JavaScript/ES6 könnte wie folgt aussehen Ich wollte es der Vollständigkeit halber aufnehmen. Dies ist eine Abbildung von Future
-Promise
von Scala.js docs genommen:
+-----------------------------------+------------------------+-------------------------------------------------------------------------------------------------------+
| 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. |
+-----------------------------------+------------------------+-------------------------------------------------------------------------------------------------------+
keine Details haben, aber die einfachsten Möglichkeiten ist das Versprechen auf JS Seite von Funktionen zu machen. Zum Beispiel die q Versprechen Bibliothek hat: q.Promise (Funktion (auflösen, ablehnen) {...}) –
Haben Sie gesehen, wie Scala.js Scala Futures zu JS verspricht und konvertiert? –
Ich denke, das Play Framework hat dafür eine Bibliothek. play.lib.F.Promise hat eine Methode "wrap", die eine in eine Zukunft eingewickelte Verheißung erzeugt und "eingewickelt", die die in eine Zukunft eingewickelte Verheißung zurückgibt. – Chronos