future 模型和 promise 模型的区别

2019-11-15 17:55:19 +08:00
 schecter

用过 py 和 js,py 的 asyncio 使用的是 future 模型,es6 有 promise,感觉我理解两者都代表一个未来的结果,但 promise 中可以自己设置结果是否成功。不过目前看到的例子 promise 感觉都可以用 future 代替,js 中是因为需要层层回调所以只能从 promise 的处理函数开始一直往下传递成功和失败的回调函数,可以理解 future 的抽象层次更高吗?两者的应用场景具体有什么区别呢?

顺便一提。本来是打算在知乎上问的,标题和描述完全和上面一样,但是问题一直过不了审。。。说描述不清。。。。难道现在知乎运营看不懂的问题都不能提了吗,气人

4531 次点击
所在节点    编程
1 条回复
mcfog
2019-11-15 18:43:54 +08:00
https://en.wikipedia.org/wiki/Futures_and_promises

> The terms future, promise, delay, and deferred are often used interchangeably, although some differences in usage between future and promise are treated below. Specifically, when usage is distinguished, a future is a read-only placeholder view of a variable, while a promise is a writable, single assignment container which sets the value of the future. Notably, a future may be defined without specifying which specific promise will set its value, and different possible promises may set the value of a given future, though this can be done only once for a given future. In other cases a future and a promise are created together and associated with each other: the future is the value, the promise is the function that sets the value – essentially the return value (future) of an asynchronous function (promise). Setting the value of a future is also called resolving, fulfilling, or binding it.

区分使用的时候,他们也是表示同一个模式的两个不同的部分,不存在两种不同的模型

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/619989

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX