Promise() as text in Label
-
hello,
considering:
Repeater { count: 999 function calculateDate(input) { var result = something_this_takes_too_much_of_the_time * input; } Label { text: return (new Promise(resolve => calculateDate(somevalue) )).then(text => text) } }
as far as await function does not work in QML, Promise is good alternative not to block GUI when calculateDate() is calculating and text should be slowly populating one by one
- this is at least my udnerstanding of Promise(), never used it before
unfortunatelly, this does not work, it gives me error:
Unable to assign QJSValue to QString
when I try:
return (new Promise(resolve => calculateDate() )).then(text => text.toString())
return (new Promise(resolve => calculateDate().toString() )).then(text => text)
but:
return (new Promise(resolve => calculateDate() )).then(text => text).toString()
only display text as "[object Promise]"
so how do I achive what I am looking for?
-
You don't. That's not how promises work.
then
doesn't return a value, it takes a function that will be eventually called and returns the promise itself (theQJSValue
or[object Promise]
you see).
Also you need to call resolve when writing a promise to indicate it is finished : (resolve => resolve(calculateDate)
));More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
What you could try doing is declaring a new Binding and use the fact that it can change a property, something like the following:
// PromiseBinding import QtQml Binding { property var promise onPromiseChanged: promise.then(v => value = v) }
Usage would be like this:
Label { PromiseBinding on text { promise: new Promise (resolve => resolve(calculateDate().toString())) } }
Disclaimer: I haven't tried that.