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

上下文维护。

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

最新文章

  1. css的初始化样式
  2. JAVA多线程超时加载当网页图片
  3. 给UIWebView调整UserAgent字段
  4. macaca运行报错之chrome-driver问题处理,关闭 Chrome 的自动更新
  5. Windows下mysql自动备份的最佳方案
  6. 只能在执行 Render() 的过程中调用 RegisterForEventValidation(RegisterForEventValidation can only be called during Render();
  7. linux系统文件属性及企业精典故障案例
  8. [转] SSH 密钥认证机制
  9. 关于“minSdk>deviceSdk”解决办法
  10. MySQL存储过程--带参数报错1064
  11. Shiro【授权、整合Spirng、Shiro过滤器】
  12. Oracle常见错误:ORA-06550、ORA-00911、ORA-02085
  13. MT【267】第一次很重要
  14. c语言学习5
  15. Hadoop HA方案调研
  16. 【Eclipse】一个简单的 RCP 应用 —— 显示Eclipse 的启动时间。
  17. Java中浮点类型的精度问题 double float
  18. 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化
  19. Linq基础操作之Select,Where,OrderBy,ThenBy源码分析
  20. mysql innodb 数据表不存在

热门文章

  1. CTF必备技能丨Linux Pwn入门教程——ROP技术(上)
  2. Django 简单评论实现
  3. pipenv 管理虚拟环境
  4. 10-剑指offer: 数值的整数次方
  5. 201871010123-吴丽丽《面向对象程序设计(Java)》第十五周学习总结
  6. 201871010101-陈来弟《面向对象程序设计(Java)》第八周学习总结
  7. CSRF/XSRF 跨站请求伪造
  8. java+selenium3学习
  9. day16_7.18 常用模块
  10. woocommerce如何隐藏/显示product meta