JohnMurray / cpp-vs

An educational site for learning C++
https://www.cpp-vs.com
Other
10 stars 1 forks source link

JS: Promises/Futures #7

Open semmel opened 6 years ago

semmel commented 6 years ago

I would contribute another piece comparing C++ Future Extensions as already available via the Boost Futures implementation vs. native JavaScript promises.

Should be fun discovering their similarities. E.g. future<X>::then([](future<X>){...}) <=> promise.then(function(x){ ... }))

Good idea?

Edit (August, 1st): Just to collect ideas here:

Feature JS C++
eager execution new Promise(computation) std::async(computation)
lazy execution function task(){ return promise; } std::packaged_task(computation)
OR combination Promise.race([]) boost::when_any()
AND combination Promise.all([]) boost::when_all()
consumption promise.then boost::future<>::then
success factory Promise.resolve boost::make_ready_future
failure factory Promise.reject boost::make_exceptional_future

Edit (Aug. 21st): Found a C++ library for the Promises A+ Spec: xhawk18/promise-cpp.

JohnMurray commented 6 years ago

Hey @semmel I think this is an awesome idea, I'm just not actively working on this project at the moment. Although I plan to get back to it in the next couple of weeks, splitting my time between it and another long-term project. If you'd like to start putting something together, we can definitely work on this.

I also have a laundry list of items I'd like to add to the comparisons, so I'll see if I can't make some tickets over the next week.

JohnMurray commented 6 years ago

Hey @semmel, I'm starting back up on this project. However, for what I'm calling "v1", I'm going to be focusing on getting some examples up in other languages (Go and Ruby). If you want to continue working on some JavaScript ideas, I'm all for it. Futures is one thing I'll be comparing in Go as well, so anything you contribute here, I can likely steal/re-use in the Go code (or vice-versa).

semmel commented 6 years ago

Unfortunately C++ Future extensions (i.e. future::then, when_all) are still under std::experimental and heavily discussed. C++11 Futures itself are too different from JS Futures.

Although boost::thread embraces these extensions, as in the JS community, many „purists“ think that this API is not perfect for async programming in C++ and that one should use better coroutines.

I don’t agree because perfect APIs are seldom easy to understand and thus are seldom popular.

I don‘t know Go and Ruby. Are their „Futures“ also asynchronous like in JS or blocking like in C++11?

Anyway I have some boost::future code and A LOT of JS promise code ready to write up this piece...

JohnMurray commented 6 years ago

Yeah, I'd prefer to avoid any extreme point of views and go with what is popular and commonly used. Since the goal of this site is to educate people about C++, I think it would be a disservice to pick a "better", but less well-known implementation/abstraction for any of the examples. I'm okay with pointing out different approaches in the description files, but the code-examples themselves should be pragmatic and useful to any new C++ developer.

It sounds like you have a ton of thoughts/ideas on this, so I'll assign this ticket to you for now. If at any point you decide you don't want to or don't have time to contribute the code, feel free just to ping me on here.

The Go and Ruby examples may not match the JS examples exactly and I probably won't cover futures in Ruby. Go uses CSP style concurrency, but futures can be implemented as an abstraction on top of that. However, Go lacks good facilities for implementing reusable abstractions, so I'll likely build on that as an example of where C++ may be more flexible.