Javascript-like Promises
-
Hi all, I just discovered this forum section and I thought I would post a small library I created to be able to do async programming easily with Qt.
https://github.com/flaviotordini/promises
I would really love to know how you're doing async (not multithreaded!) programming in Qt. For example, do you use QFuture? How?
-
@flaviotordini said in Javascript-like Promises:
how you're doing async (not multithreaded!) programming in Qt
By using signals and slots. Qt is an asynchronous framework. Most of the time you don't have to deal with threads.
"For example, do you use QFuture? How?" - QFuture is only needed if you're working with threads, which you do not want to. How to use it is explained in the documentation: https://doc.qt.io/qt-6/qfuture.html
-
I'm very aware of signals and slots. IMO JS-like promises solve a slightly different problem. Qt signals are shared among all subscribers. Promises are specific to a single function call.
In my lib I implemented promises using signal and slots. The difference is that there is a new QObject for each function call. In my experience this prevents potential issues with clients connecting to the same signal in the same object and then receiving multiple signals or having to manually disconnect after the first signal...
Also signals and slots are scattered in the class header and in many functions, while the promise approach is more self-contained.