-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API question: Is there value_or(...) equivalent? #49
Comments
You are trying to retrieve an asynchronous promise result in a syncronous way. There is an example for this in the tests:
But generally, this is not what promises are made for. I'd even go as far as saying that using |
@pwuertz What I have is an async API which I need to make (ocasionally) synchronous to interoperate with the rest of the system. To achieve this, I'd need some equivalents of The question I'm trying to ask is whether these kind of member functions would be useful to have in the API (instead of having my own free functions that would do the same). |
I'm not sure it would be really useful since, as @pwuertz mentioned, the I see that bluebird provides APIs to inspect the promise values so it may make sense to have similar methods. However,
I don't get how an optional would make things better (you would still need to call |
There is indeed one good example I came across recently. I've been playing around with C++ co-routines and implemented a The spec defines a
Indeed this is also what python does with |
@pwuertz @simonbrunel Could you advise what would be the best way to implement a I'm asking because I'm facing exactly these kind of reentrancy issues when calling I was trying to think on an answer, and the two ideas I have:
|
Generally, there is no way to do this. Think of it like some sort of logical-fallacy. You can't (and shouldn't) block-wait on something that relies on being used in a non-blocking context.
Asynchronous programming is extremely 'viral'. The only viable way to do this is by refactoring the synchronous code, e.g. by making it async or by using classic continuation patterns like callbacks.
This may work in specific situations, but can't work in general. In Qt for example there is only one GUI/main thread. If you block this one, any promise waiting for a signal from there won't ever resolve. In order to do this safely, you as a developer require exact knowledge about which promise is safe to be-sync-blocked at which line using which thread.
This is a general design issue which isn't specific to QPromise. As a reference, you can't sync block Python Async-Coros or JavaScript async functions either. |
@mkucmpro |
I wanted to know if there exists an API similar to
value_or(...)
that would allow me to do something like:I often find myself doing something similar to:
Would an addition like
value_or()
be useful for the library?Btw. I was also thinking that
to_optional()
could also fit my needs but that would require C++17 (and AFAIK the library aims to support C++11). What is the authors' current attitude towards conditional compilation and feature testing (if the compiler supports C++17)?The text was updated successfully, but these errors were encountered: