异步编程的类型系统: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

 

posted @ 2019-10-22 16:44  zzfx  阅读(263)  评论(0编辑  收藏  举报