异步编程类型的结构和操作。

上下文维护。

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----异步编程类型的结构和操作的更多相关文章

  1. 并发编程(三)Promise, Future 和 Callback

    并发编程(三)Promise, Future 和 Callback 异步操作的有两个经典接口:Future 和 Promise,其中的 Future 表示一个可能还没有实际完成的异步任务的结果,针对这 ...

  2. C++11并发编程:async,future,packaged_task,promise

    一:async std::async:用于创建异步任务,可以代替创建线程,函数原型:async(std::launch::async | std::launch::deferred, f, args. ...

  3. node.js异步编程解决方案之Promise用法

    node.js异步编程解决方案之Promise var dbBase = require('../db/db_base'); var school_info_db = require('../db/s ...

  4. JavaScript异步编程助手:Promise模式

    :Promises是一种令代码异步行为更加优雅的抽象,它很有可能是JavaScript的下一个编程范式,一个Promise即表示任务结果,无论该任务是否完成. 异步模式在Web编程中变得越来越重要,对 ...

  5. Promise是如何实现异步编程的?

    Promise标准 不能免俗地贴个Promise标准链接Promises/A+.ES6的Promise有很多方法,包括Promise.all()/Promise.resolve()/Promise.r ...

  6. 并发编程 05—— Callable和Future

    Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...

  7. Java 并发编程:Callable和Future

    项目中经常有些任务需要异步(提交到线程池中)去执行,而主线程往往需要知道异步执行产生的结果,这时我们要怎么做呢?用runnable是无法实现的,我们需要用callable实现. import java ...

  8. vertx 异步编程指南 step8-使用RxJava进行反应式编程

    vertx 异步编程指南 step8-使用RxJava进行反应式编程 2018-04-23 13:15:32 zyydecsdn 阅读数 1212  收藏 更多 分类专栏: vertx   到目前为止 ...

  9. 【高并发】两种异步模型与深度解析Future接口

    大家好,我是冰河~~ 本文有点长,但是满满的干货,以实际案例的形式分析了两种异步模型,并从源码角度深度解析Future接口和FutureTask类,希望大家踏下心来,打开你的IDE,跟着文章看源码,相 ...

随机推荐

  1. html 实体编码转换成原字符

    今天遇到件很恶心的事,某国外歌词网站提供的歌词在源文件里使用“&#数字;”格式的编码表示abcd....原来小菜我实在才疏学浅不知此为何物,于是特有的搜索引擎控,搜之.片刻得解,此乃html实 ...

  2. CTF必备技能丨Linux Pwn入门教程——栈溢出基础

    这是一套Linux Pwn入门教程系列,作者依据i春秋Pwn入门课程中的技术分类,并结合近几年赛事中出现的一些题目和文章整理出一份相对完整的Linux Pwn教程. 课程回顾>>Linux ...

  3. HLAPI

    和SPS硬件交互的API

  4. qt需求

    1.登录框 账号密码 复杂度判断 2.监控  权限认证 (5分钟必须锁定,退回登录状态)--b/s 3.exe 嵌入exe (什么时间触发了什么消息,任何消息触发都可以,日志文件zlog,5分钟没有写 ...

  5. http异步通信

    get 请求1)创建一个XMLHttpRequest对象2)调用该对象的open方法3)如果是get请求,设置回调函数onreadystatechange = callback4)Send如果是pos ...

  6. 201871010112-梁丽珍《面向对象程序设计(java)》第十一周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  7. HDFS 分布式文件系统

    博客出处W3c:https://www.w3cschool.cn/hadoop/xvmi1hd6.html 简介 Hadoop Distributed File System,分布式文件系统 架构 B ...

  8. 如何做到MySQL高扩展性?

    高并发及其关注要点 近年来,随着互联网.移动互联网的飞速发展,业务系统的互动性日益增强,用户规模不断攀升,电商.游戏.直播.在线教育.短视频等一系列新兴移动端应用如雨后春笋般涌现出来,这些应用 “高并 ...

  9. python27期day12:推导式、内置函数、高阶函数、匿名函数、作业题

    1.推导式:做一些有规律的数据结构 列表推导式: 普通循环模式: [加工后的变量 for 循环] 示例一:print([i for i in range(1,51)]) 结果:[1, 2, 3, 4, ...

  10. golang和python互相调用

    http://blog.yuanzhaoyi.cn/2018/06/27/golang_python.html python3-ctypes: https://docs.python.org/3.5/ ...