macomphy
macomphy

Reputation: 350

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

I am a c++ programmer and tried to study std::future and std::promise these days. When I randomly search some information about future/promise, I found some 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 proposal have mentioned it. So, there are two question:

  1. does std::future in c++ corresponding to promise in javascript?
  2. if 1 is true, why they confused future and promise?

Upvotes: 5

Views: 641

Answers (2)

Karl Bielefeldt
Karl Bielefeldt

Reputation: 49058

Dynamically-typed languages sometimes combine concepts that are separated in statically-typed languages. A JavaScript promise is basically both a C++ promise and future. The resolve and reject functions in JavaScript roughly correspond to the promise side in C++, and the then function in JavaScript roughly corresponds to the future side in C++.

then also can compose promises in JavaScript, which you might consider a third feature, but in practice futures aren't very useful without composition.

Upvotes: 1

Quimby
Quimby

Reputation: 19123

  1. Yes.
  2. std::future<T> stands for a future result of T, i.e. the object will at some point in the future hold a T. std::promise<T> is an object promising to provide a T at some point in the future.

Which language got the naming right is debatable.

Upvotes: 4

Related Questions