加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

是否有F#等同于Scala的承诺?

发布时间:2020-12-16 09:00:56 所属栏目:安全 来源:网络整理
导读:在F#中有类似 Scala’s Promise的东西吗? While futures are defined as a type of read-only placeholder object created for a result which doesn’t yet exist,a promise can be thought of as a writeable,single-assignment container,which complete
在F#中有类似 Scala’s Promise的东西吗?

While futures are defined as a type of read-only placeholder object
created for a result which doesn’t yet exist,a promise can be thought
of as a writeable,single-assignment container,which completes a
future. That is,a promise can be used to successfully complete a
future with a value (by “completing” the promise) using the success
method. Conversely,a promise can also be used to complete a future
with an exception,by failing the promise,using the failure method.

Async的内容涵盖了部分内容,但如果您的代码在Async环境之外运行,则Promises是一个方便的工具. (例如,您可以执行诸如在UI线程中完成Promise之类的操作 – 即使UI环境对Async一无所知也是如此.)

解决方法

.Net相当于一个承诺是 TaskCompletionSource,所以你可以从F#中使用它们.您可以创建Async< T>来自任务< T>使用 Async.AwaitTask,例如

let tcs = new TaskCompletionSource<int>()
let ta: Async<int> = Async.AwaitTask tcs.Task

//complete completion source using `SetResult``SetException` etc.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读