c++ futures/promises like javascript?
c++, promise
Solution
A `.then` function for `std::future` has been proposed for the upcoming C++17 standard.
Boost's implementation of future (which is compliant with the current standard, but provides additional features as extensions) already provides parts of that functionality in newer versions (1.53 or newer).
For a more well-established solution, take a look at the Boost.Asio library, which does allow easy implementation of asynchronous control flows as provided by `future.then`. Asio's concept is slightly more complicated, as it requires access to a central `io_service` object for dispatching asynchronous callbacks and requires manual management of worker threads. But in principle this is a very good match for what you asked for.
Problem
I've been writing some javascript and one of the few things I like about the environment is the way it uses promises/futures to make handlers for asynchronous events. In C++ you have to call .get on a future and it blocks until the result of the future is available but in Javascript you can write .then(fn) and it will call the function when the result is ready. Critically it does this in the same thread as the caller at a later time so there are no thread synchronization issues to worry about, at least not the same ones as in c++. I'm thinking in c++ something like - ``` auto fut = asyncImageLoader("cat.jpg"); fut.then([](Image img) { std::cout << "Image is now loaded\n" << image; }); ``` Is there any way to achieve this in c++? Clearly it will need some kind of event queue and event loop to handle dispatching the callbacks. I could probably eventually write the code to do most of this but wanted to see if there was any way to achieve the goal easily using standard facilities.