本文介绍了Js Deferred/Promise/Future 与 Scala 等函数式语言相比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主要使用 Scala 和 JavaScript 等编程语言.我试图了解在两种语言中如何使用异步反应式编程的异同.你能帮我吗?

I'm mostly using programming languages like Scala and JavaScript. I'm trying to understand the similarities and differences in how async reactive programming is used in both languages. Can you help me?

我没有采用任何特定的 Js Promise 框架,因为它似乎许多实现了类似的规范(如 Promise/A).我目前只用过 Q.

I'm not taking any particular Js Promise framework because it seems many implement the similar specifications (like Promise/A). I've only used Q so far.

似乎在 Javascript 中,我们将 Deferred 称为我们解析以完成 Promise 的对象.在 Scala 中,Promise 似乎是您解析为获得 Future monad 的对象.

It seems that in Javascript we call a Deferred the object we resolve to complete a Promise.In Scala, it seems the Promise is the object you resolve to get a Future monad.

谁能告诉我这是否正确?在 Js 和 Scala 之间对术语 Promise 的不同用法有什么好的理由吗?

Can someone tell me if this is right? Is there any good reason for a different usage of the term Promise between Js and Scala?

此外,在 Scala 中,我们通常使用 mapflatMap(也称为 bind 在 Haskell 中).这些在 Js 中的等价物是什么?

Also, in Scala we usually chain Future monads with further computations using operators like map and flatMap (also called bindin Haskell). What is the equivalent of these in Js?

我可能错了,但在我看来,在 Js 中,Promise 上的 then 可以同时处理 mapflatMap 操作符对吗?如果是这样,是否有可能在Js中获得promise of result?就像我们可以在 Scala 中得到一个 Future[Future[Result]](无论如何都可以将其展平为一个 Future[Result]).

I may be wrong but it appears to me that in Js the then on a Promise kind of handle both map and flatMap operators right? If so, is it possible to obtain a promise of promise of result in Js? Like we can get a Future[Future[Result]] in Scala (which can be flattened to a Future[Result] anyway).

Js Promise 是 monad 吗?即使方法名称与我们在 monad 文献中找到的名称并不真正匹配,也似乎如此.

Is Js Promise a monad? It kind of seems so even if the method names do not really match those we find on monad literature.

推荐答案

是,也不是.

虽然极其相似.使用符合 Promises/A+ 规范的 JavaScript Promises .then 并不是真正的 monadic 绑定,而是 .map.flatMap 两者.在 .then 处理程序中,当你返回一个 promise 时,它​​会递归地解包它.

Yes, and no.

While extremely similar. With JavaScript Promises that comply to the Promises/A+ spec .then is not really a monadic bind and does .map and .flatMap both. Inside a .then handler when you return a promise it will recursively unwrap it.

Promise.delay(1000).then(function() {
    return Promise.delay(1000).then(function () {
        return Promise.delay(2000);
    }).then(function () {
        return Promise.delay(5000)
    });
}).then(function () {
    alert("This is only shown after 8 seconds and not one");
});

(小提琴)

您是对的,标准 JS 承诺库和 A+ 规范不具有 monadic 承诺.它们已被讨论过,并且存在诸如 fantasy-promises 之类的实现.他们遵循不同的规范并且很少采用.另请参阅.在语言设计讨论论坛上一直在讨论它 - esdiscuss 和一个不进行平面映射并允许 monadic promise 的 monadic .chain 方法被考虑但不太可能实现.

You are correct that the standard JS promise libraries and the A+ spec does not feature monadic promises. They have been discussed, and implementations like fantasy-promises exist. They follow a differnet spec and have little adoption. Also see this. There has been ongoing discussion about it in the language design discussion forum - esdiscuss and a monadic .chain method that does not flatmap and allows for monadic promises is considered but unlikely to make it.

这是出于务实的原因.当前实现 promise 的方式非常有用.很少有你真正想要一个 Future[Future 的情况,通常你希望延续只是在语言中工作.Promise 从 monad 中借用"并且在某种意义上是monadic"..then 非常 很接近绑定,在我的脑海中我可以互换使用它们:)

This is for pragmatic reasons. The current way promises are implemented is immensely useful. Rare are the cases you actually want a Future[Future and normally you want continuations to just work in the language. Promises 'borrow' from monads and are 'monadic' in a sense themselves. .then is very close to bind and in my head I use them interchangeably :)

在大多数 Promise 库的 Scala 中,不可能像 Future[Future[Value]] 那样拥有 Promise[Promise[Value]].您必须将它包装在一个对象中并拥有 Promise[Container[Promise[Value]]].

It is impossible to have a Promise[Promise[Value]] like a Future[Future[Value]] in Scala with most promise libraries. You'd have to wrap it in an object and have Promise[Container[Promise[Value]]].

Promise.delay(1000).then(function () {
    return Promise.delay(1000).then(function () {
        return {
            wrap: Promise.delay(2000).then(function () {
                return Promise.delay(5000);
            })
        };
    });
}).then(function () {
    alert("This logs after 1 second");
    // I've also not seen a really solid use case
    // except TypeScript type inference which is meh
});

(小提琴)

两者之间还有许多其他较小的差异,但通常您的断言是正确的.

There are also a number of other smaller differences between the two, but generally you are correct in your assertions.

这篇关于Js Deferred/Promise/Future 与 Scala 等函数式语言相比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 20:30
查看更多