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

future 模型和 promise 模型的区别

  •  
  •   schecter · 2019-11-15 17:55:19 +08:00 · 4429 次点击
    这是一个创建于 1617 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    mcfog
        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.

    区分使用的时候,他们也是表示同一个模式的两个不同的部分,不存在两种不同的模型
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2575 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:24 · PVG 23:24 · LAX 08:24 · JFK 11:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.