用过 py 和 js,py 的 asyncio 使用的是 future 模型,es6 有 promise,感觉我理解两者都代表一个未来的结果,但 promise 中可以自己设置结果是否成功。不过目前看到的例子 promise 感觉都可以用 future 代替,js 中是因为需要层层回调所以只能从 promise 的处理函数开始一直往下传递成功和失败的回调函数,可以理解 future 的抽象层次更高吗?两者的应用场景具体有什么区别呢?
顺便一提。本来是打算在知乎上问的,标题和描述完全和上面一样,但是问题一直过不了审。。。说描述不清。。。。难道现在知乎运营看不懂的问题都不能提了吗,气人
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. 区分使用的时候,他们也是表示同一个模式的两个不同的部分,不存在两种不同的模型 |