c++ 中的 future 是否对应于 javascript 中的 promise?

Does future in c++ corresponding to promise in javascript?

我是c++程序员,这几天在努力研究std::futurestd::promise。随便搜了一下future/promise的资料,发现有discussion about future/promise in javascript and promise in javascript has then function. In c++, even though std::future don't have then function now, but some 提到了。所以,有两个问题:

  1. c++中的std::future是否对应javascript中的promise?
  2. 如果 1 为真,为什么他们混淆了 future 和 promise?
  1. 是的。
  2. std::future<T>代表T的未来结果,即对象将在未来的某个时刻持有Tstd::promise<T> 是一个对象 承诺在将来某个时候提供 T

哪种语言获得了命名权还有待商榷。

Dynamically-typed 语言有时会结合 statically-typed 语言中分离的概念。 JavaScript promise 基本上既是 C++ promise 又是 future。 JavaScript中的resolvereject函数大致对应C++中的promise端,JavaScript中的then函数大致对应C++中的future端.

then 也可以在 JavaScript 中组合 promise,您可能会认为这是第三个功能,但在实践中,如果没有组合,futures 并不是很有用。