异步编程的类型系统:promise & future & closure & observable----异步编程类型的结构和操作
异步编程类型的结构和操作。
上下文维护。
A promise represents the eventual result of an asynchronous operation. The primary way of interacting with a promise is through its then method, which registers callbacks to receive either a promise's eventual value or the reason why the promise cannot be fulfilled.
https://github.com/promises-aplus/promises-spec
A Promise is a placeholder object that represents the result of an async operation. This object will hold the information about the status of the async operation and will notify us when the async operation succeeds or fails.
https://cn.bing.com/search?q=async+promise&qs=AS&pq=async+pro&sc=8-9&cvid=577FC65F218A4BC0A638F0403A5C606F&FORM=BESBTB&sp=1&ensearch=1
https://www.12devs.co.uk/articles/promises-an-alternative-way-to-approach-asynchronous-javascript/
According to wikipedia, they are the same concept:
Some libraries may choose to call them one way, some may choose to call them another. And each time, they may be implemented in different flavors. Some libraries may choose to use these synonyms to distinguish different flavors. While I would argue that this is a bad choice (because evidently it confuses people), this link suggests that in Scala this common practice.
As @Ptharien's Flame suggested, in Scala a Future is a read-only operation, while a Promise gives you the ability to yield a result (or failure) for the operation it represents.
A Promise is thus best used by the code responsible to carry out the operation to propagate the result, while a Future is used to expose it to client code, that will in turn await the result. But again, please note that this distinction is Scala specific and may confuse outsiders.
https://softwareengineering.stackexchange.com/questions/207136/what-is-the-difference-between-a-future-and-a-promise
异步操作的有两个经典接口:Future 和 Promise,其中的 Future 表示一个可能还没有实际完成的异步任务的结果,针对这个结果可以添加 Callback 以便在任务执行成功或失败后做出对应的操作,而 Promise 交由任务执行者,任务执行者通过 Promise 可以标记任务完成或者失败。
Future表示一个可能还没有实际完成的异步任务结果;实际在编程中,应用future数据结构的时候,你得到并不是一个真实结果;而是一个FutureData; 真实的结果可能还没有处理完成。当然你可以针对这个结果添加Callback操作(成功或者失败的回调)。
Promise结构如同一个状态机,是基于状态的流程控制结构;在不同的状态过程中,由任务执行提供相应的事务来决定在这个状态的时候,程序该做什么;
Promise有4种状态:
- pending: 还没有得到肯定或者失败结果,进行中
- fulfilled: 成功的操作
- rejected: 失败的操作
- settled: 已被 fulfilled 或 rejected
提供方法:
- then:将事务添加到事务队列中
- resolve:开启流程,让整个操作从第一个事务开始执行
https://www.cnblogs.com/monion/p/6110703.html
异步编程的类型系统:promise & future & closure & observable----异步编程类型的结构和操作的更多相关文章
- 并发编程(三)Promise, Future 和 Callback
并发编程(三)Promise, Future 和 Callback 异步操作的有两个经典接口:Future 和 Promise,其中的 Future 表示一个可能还没有实际完成的异步任务的结果,针对这 ...
- C++11并发编程:async,future,packaged_task,promise
一:async std::async:用于创建异步任务,可以代替创建线程,函数原型:async(std::launch::async | std::launch::deferred, f, args. ...
- node.js异步编程解决方案之Promise用法
node.js异步编程解决方案之Promise var dbBase = require('../db/db_base'); var school_info_db = require('../db/s ...
- JavaScript异步编程助手:Promise模式
:Promises是一种令代码异步行为更加优雅的抽象,它很有可能是JavaScript的下一个编程范式,一个Promise即表示任务结果,无论该任务是否完成. 异步模式在Web编程中变得越来越重要,对 ...
- Promise是如何实现异步编程的?
Promise标准 不能免俗地贴个Promise标准链接Promises/A+.ES6的Promise有很多方法,包括Promise.all()/Promise.resolve()/Promise.r ...
- 并发编程 05—— Callable和Future
Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...
- Java 并发编程:Callable和Future
项目中经常有些任务需要异步(提交到线程池中)去执行,而主线程往往需要知道异步执行产生的结果,这时我们要怎么做呢?用runnable是无法实现的,我们需要用callable实现. import java ...
- vertx 异步编程指南 step8-使用RxJava进行反应式编程
vertx 异步编程指南 step8-使用RxJava进行反应式编程 2018-04-23 13:15:32 zyydecsdn 阅读数 1212 收藏 更多 分类专栏: vertx 到目前为止 ...
- 【高并发】两种异步模型与深度解析Future接口
大家好,我是冰河~~ 本文有点长,但是满满的干货,以实际案例的形式分析了两种异步模型,并从源码角度深度解析Future接口和FutureTask类,希望大家踏下心来,打开你的IDE,跟着文章看源码,相 ...
随机推荐
- vue学习指南:第七篇(详细) - Vue的 组件通信
Vue 的 父传子 子传父 一.父组件向子组件传值: 父传子 把需要的数据 传递给 子组件,以数据绑定(v-bind)的形式,传递到子组件内部,供子组件使用 缩写是(:) 1.创建子组件,在src/ ...
- GIC , SPI , PPI (窝窝科技的文章题目改了下)【转】
转自:https://www.cnblogs.com/tureno/articles/6403408.html 转载于: http://www.wowotech.net/irq_subsystem/ ...
- Linux下用火焰图进行性能分析【转】
转自:https://blog.csdn.net/gatieme/article/details/78885908 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原 ...
- 【面试题】了解session和cookie吗?
问题:SESSION与COOKIE的区别? 1.cookie数据存放在客户的浏览器上,session数据放在服务器上. 2.cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKI ...
- bert,albert的快速训练和预测
随着预训练模型越来越成熟,预训练模型也会更多的在业务中使用,本文提供了bert和albert的快速训练和部署,实际上目前的预训练模型在用起来时都大致相同. 基于不久前发布的中文数据集chineseGL ...
- LVS---linux虚拟服务(一)
一.集群cluster 1.集群的产生 2.集群的类型 3.分布式系统 4.集群和分布式的区别 二.LVS 1.为什么使用LVS? 2.LVS术语 3.LVS架构 4.LVS集群的类型 5.关于会话保 ...
- docker 持久化存储
1.data Volume mysql5.7:dockerfile FROM debian:stretch-slim # add our user and group first to make s ...
- python27期day10:函数的动态参数、函数的注释、函数的名称空间、函数的嵌套、global(修改全局的)、nonlocal(修改局部的)、函数名的第一类对象及使用、作业题。
1.动态参数的作用: 能够接收不固定长度参数 位置参数过多时可以使用动态参数 * args是程序员之间约定俗称(可以更换但是不建议更换) * args获取的是一个元组 ** kwargs获取的是一个字 ...
- TCP数据报结构以及三次握手(九)
TCP(Transmission Control Protocol,传输控制协议)是一种面向连接的.可靠的.基于字节流的通信协议,数据在传输前要建立连接,传输完毕后还要断开连接. 客户端在收发数据前要 ...
- USACO Sabotage
洛谷 P2115 [USACO14MAR]破坏Sabotage https://www.luogu.org/problem/P2115 JDOJ 2418: USACO 2014 Mar Gold 2 ...