Quick Promise - QML Promise Library
-
Hi,
In case you are looking for a Promise library that works well with QML , I would like to share my latest open source project with you. Its name is “Quick Promise”. It is a library that provides Promise object in QML way. It comes with a Promise component with signal and property. It could be resolved via a binary expression , another promise object, then trigger your callback by QueuedConnection.
Moreover, it also provides Promise as Javascript object that don’t need to declare in QML way. The API is fully compliant with Promises/A+ specification (Passed all the test cases) and therefore it just works like many other Promise solutions for Javascript application.
Example:
Promise { resolveWhen: !someService.running // Once the expression become truth , the onFulfilled signal will be triggered via QueuedConnection onFulfilled: { } } Promise { rejectWhen: timer.onTriggered // Reject after timeout onRejected: { } }
The project link: QuickPromise
Enjoy it.
-
Hi,
You project reminds me of AngularJS. Looks pretty interesting ! Thanks for sharing
-
@SGaist Thx
haha, you also reminds me the time when I shift from AngularJS to QML. I tried to understand/write QML in an AngularJS way. Look for a solution of "Transclude" in QML. Assume the item in ListModel support 2 way data binding...
-
v1.0.2 has been just released. Now user could resolve a promise by multiple signals and binary expression!
Example
- Resolve by multiple signals.
Promise { resolveWhen: Q.all([timer.triggered, loader.loaded]); }
- Resolve by signal and binary expression
Promise { resolveWhen: Q.all([timer.triggered, promise2]); Promise { id : promise2 resolveWhen: image.status === Image.Ready } }
-
v1.0.3 released.
Changes
- The resolve parameter to promise generated by Q.all() become an array of results of original promises
-
This looks pretty promising (pun possibly intended).
I've been looking for something like this to avoid callback hell. However, I need to resolve/reject based off an argument in a signal.ie.
Item { id: obj signal done(bool success) } Promise { id : signalArgPromise resolveWhen: obj.done(success === true) rejectWhen: obj.done(success === false) }
Would it be possible to do something like this using your library?
-
@cp_mark QML don't support this kind of syntax. So it should either to create signals like success / fail or check the value during the callback function:
onDone: { if (success) { signalArgPromise.resolve(); } else { signalArgPromise.reject(); } }