首页   注册   登录
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Coding
V2EX  ›  编程

future 模型和 promise 模型的区别

  •  
  •   schecter · 30 天前 · 1597 次点击

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

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

        1
    mcfog   30 天前
    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.

    区分使用的时候,他们也是表示同一个模式的两个不同的部分,不存在两种不同的模型
    关于   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   2317 人在线   最高记录 5043   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.3 · 16ms · UTC 10:59 · PVG 18:59 · LAX 02:59 · JFK 05:59
    ♥ Do have faith in what you're doing.