与Scala等函数式语言相比,Js延迟/承诺/未来

编程入门 行业动态 更新时间:2024-10-23 13:35:07
本文介绍了与Scala等函数式语言相比,Js延迟/承诺/未来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我主要使用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中我们称之为延期我们决定完成 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中,我们通常使用 map 等运算符进行进一步计算的 Future monads链接 flatMap (在Haskell中也称为 bind )。在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?

我可能错了,但在我看来,在上的然后 承诺处理 map 和 flatMap 运算符对吗?如果是这样,是否有可能在Js中获得承诺结果?就像我们可以在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 承诺 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 +规范 .then 的JavaScript Promise实际上不是monadic绑定,并且 .map 和 .flatMap 两者。当你返回一个承诺时,在一个 .then 处理程序内,它会以递归方式解包它。

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 .chain 方法,它不会平面映射并允许monadic promises被考虑但不太可能成功。

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.

这是出于实用的原因。实施当前的承诺方式非常有用。很少见的是你真的想要一个 Future [Future ,通常你希望继续只使用这种语言。承诺从单子借,从某种意义上说是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 [Promise [Value]] 不可能像 Future [Future [Value]] 在Scala中有大多数承诺库。你必须将它包装在一个对象中并且 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.

更多推荐

与Scala等函数式语言相比,Js延迟/承诺/未来

本文发布于:2023-11-25 02:46:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628010.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   语言   未来   Scala   Js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!