什么是承诺的反面? [英] What is the inverse of a promise?

查看:155
本文介绍了什么是承诺的反面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个承诺代表一个可能在将来可用的价值(或不这样做)。



我所寻找的是一种数据类型,代表可能的值可能在将来变得不可用(可能是由于错误):

  Promise ab = TransitionFromTo< PENDING,or<值a,错误b>> 
??? a = TransitionFromTo<值a,或< ENDED,FAILED>>或
? a b = TransitionFromTo<值a,或< ENDED,error b>>

是否已经探索过这样的概念(或类似概念)?有没有现成的语义或习惯用法?



例如,它可能代表一个打开的数据库连接,将被关闭。我的具体用例是在FRP中表示一个可变的,即可变大小的集合类集合,作为这种结束值的流 - 当事件发生时,将值添加到集合中,并且值结束它被移除了。

我感觉像将它表示为 Signal< Option< value>> {data = value,ended = Promise< null>} 并不完全符合它 - 第一种情况不包括保证值最终稳定为没有,第二个数据字段在结束后仍然可以访问。

解决方案

简而言之 - 可以使用异步生成器进行建模。



在数据库连接示例中,概念上你有一个数据库连接序列,每次访问您要从序列(连接)中产生(可能是异步)值的值时。屈服可以是异步的,并且值本身也可以是异步的。该序列可能会结束(使其不再可用),也可能总是产生结果 - 它可能会保持挂起状态,并且不会再次产生另一个连接。



值得一提的是,异步生成器是一种巨大的超集,它的表达能力更强,并且不是直接反转。

反向如何?



您可以通过几种不同的方式反驳承诺。



承诺是单数 时间 getter 。它包含以下内容:


  • 它表示一个单个值。

  • 其值时间(即与时间有关)。 getter



引用Kris关于承诺时间性的工作:


观察者可以订阅,最终看到承诺的价值。他们可以在承诺有价值之前或之后做到这一点。任何数量的观察者都可以多次订阅,任何单个观察者都可以多次订阅相同的承诺。承诺是广播。没有消费者可以干预另一个消费者的法律使承诺不能中止正在进行的工作。一个承诺代表一个结果,而不是导致这个结果的工作。


每个方面的承诺都是不同的。


  • 一个延迟是一个单独的时间设置器。这是一个承诺的双重性,它允许设置一个类似于承诺获得它的值。

  • 阅读器更通常称为可观察的)是承诺的多版本,以及可迭代的时间版本。它代表了即将到来的多重价值。它就像是一个可以改变其价值的承诺。 价值是最常用和最原始的东西之一是同步版本如果你需要的东西不同于所有三种承诺 - 你需要更先进的东西。 一个生成器是与此相关的承诺的逆向,它是一个空间多值设置器。这与每个方面的承诺都是相反的。

    然而,你所说的是两方面的异步,你需要的东西是可用/不可用和更改值。这是一个异步生成器,是这里比较复杂的类型之一。


    你的类型需要类似于两次异步的生成器,一旦获得下一个值并且一次获取值本身,我已经问过一个类似的C#问题在这里。这里是一个有趣的谈话和讲座。基本上你想要一个生成器,其值 next()是异步的。它相当复杂,并且模型的适当模型很少(例如 - 滚动和内容都是异步的无限滚动)。

    从某种意义上说,序列结束表示您的值不可用,并且生成下一个实例的序列表示您的信号暂时不可用。



    我也推荐 Erik Meijer的谈话 Kris Kowal的GTOR 。 / p>

    A promise represents a value that might become available in the future (or fails to do so).

    What I am looking for is a data type which represents an available value that might become unavailable in the future (possibly due to an error):

    Promise a b = TransitionFromTo<PENDING, Either<value a, error b>>
    ??? a       = TransitionFromTo<value a, Either<ENDED, FAILED>> or
    ??? a b     = TransitionFromTo<value a, Either<ENDED, error b>>
    

    Has such a concept (or similar) been explored already? Are there existing semantics or common idioms?

    For example, it might represent an open database connection that will get closed. My particular use case would be representing a "mutable", i.e. variable-sized, set-like collections in FRP as a stream of such "ending values" - when an event occurs the value is added to the set and when the value "ends" it is removed.

    I felt like representing this as a Signal<Option<value>> or {data = value, ended = Promise<null>} doesn't quite match it - the first case doesn't include the guarantee that the value finally settles to Nothing and the second has the data field still accessible after the end.

    解决方案

    In short - It's possible to model with an Async Generator.

    In the DB connection example, conceptually you've got a sequence of DB connections, every time you access the value you're yielding (possibly asynchronously) a value from the sequence (a connection). Yielding can be asynchronous, and the value itself might be asynchronous too. The sequence might end (making it never available again) or it might always yield the result - it might remain pending and never yield another connection again.

    It's worth mentioning that an async generator is a vast superset of the type you're after - it's much more expressive and is not the diret inverse.

    In long - Inverse how?

    You could inverse a promise in several different ways.

    A promise is a singular temporal getter. That is it holds the following:

    • It represents a single value.
    • Its value is temporal (that is, time dependant).
    • It's a getter.

    Quoting Kris's work on the temporality of promises:

    An observer can subscribe to eventually see the value of a promise. They can do this before or after the promise has a value. Any number of observers can subscribe multiple times and any single observer can subscribe to the same promise multiple times.... Promises are broadcast. The law that no consumer can interfere with another consumer makes it impossible for promises to abort work in progress. A promise represents a result, not the work leading to that result.

    The inverse of a promise in each of these regards is different.

    • A Deferred is a singular temporal setter. It is the dual of a promise and it allows setting a value similarly to how a promise allows getting it.

    • A Reader (more commonly called an observable) is the multiple version of a promise, and the temporal version of an iterable. It represents multiple values that are temporally coming. It's like a promise that can change its value.

    • A Value , one of out most used and primitive things is the synchronous version of a promise.

    If you want something that is unlike a promise in all three - you need something more advanced. A Generator, is the inverse of a promise in that regard, it's a spatial, multivalued setter. It's the opposite of a promise in every such regard.

    However, what you're talking of is something that's async in both regards, you want something that is both available/unavailable and changes value. That's an async generator, one of the more complex types in play here.

    Your type needs to be similar to a generator that's async twice, once in having the next value and once in getting the value itself, I've asked a similar C# question here. Here is an interesting talk and lecture about it.

    Basically you want a generator whose values and next() are asynchronous. It's rather complex and there are few things it models properly (for example - an infinite scroll where both the scrolling and the content are asynchronous).

    In a sense, the sequence ending signals your value "not being available" anymore, and the sequence generating the next instance indicates your signal not being available temporally again.

    I also recommend Erik Meijer's talk and Kris Kowal's GTOR about this subject.

    这篇关于什么是承诺的反面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆